Log is a very important part of a system, we often need to view the log to locate problems, today we will learn about the Spring Boot log system. Many students are used to using system. out to output logs in production code, which is not recommended, because System.out is a synchronous operation, which will affect System performance to a certain extent, while Logger is an asynchronous operation.

The default logging system in Spring Boot is logback. Of course, we don’t need to reference the logback dependency, since the logback dependency is already used in spring-boot-starter.

1. Introduction to console logs

1.1 Log Level

The log levels are as follows: TRACE < DEBUG < INFO < WARN < ERROR < ALL < OFF.

If logs are set to ERROR, logs below the ERROR level will not be output.

If we want to set the log level for a package, add the following line to the POM file:

logging:
  level:
  	# the package name
    com.javatrip: warn
Copy the code

If you want to change the Spring Boot default level, change the package name to root.

logging:
  level:
    root: warn
Copy the code

1.2 Console Logs

By default, Spring Boot prints info-level logs to the console. The console output is as follows:

The description of the log output is as follows:

  • Time and date: accurate to the millisecond
  • Log level: ERROR, WARN, INFO, DEBUG or TRACE
  • The process ID
  • The separator:---Identifies the start of the actual log
  • Thread name: enclosed in square brackets (may truncate console output)
  • Logger name: The name of the class that normally uses the source code
  • Log contents

Log file output

In a real project, we would want to export logs as files to quickly locate problems. Spring the Boot on the log file information can be used to refer to the website https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-logging.

Because the standard logback.xml configuration file is loaded too early, you cannot use extensions in it. You need to either use logback-spring.xml or define a logging.config property.

Because the standard logback.xml configuration file loads too early, you can’t use extensions in it. You need to use logback-spring. XML or define the logging.config attribute.

So let’s define a logback-spring. XML file to configure the log information.

Logback-spring. XML file definition and comments:


      
<configuration>
    <! -- Log file name -->
    <property name="LOG_FILE" value="myLog" />
    <! -- Log file path -->
    <property name="LOG_PATH" value="D://log//dev" />
    <! Console log output format -->
    <property name="LOG_PATTERN_CONSOLE" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} --> %msg%n" />
    <! -- File log output format -->
    <property name="LOG_PATTERN_FILE" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} --> %msg%n" />
    <! -- Set console log -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${LOG_PATTERN_CONSOLE}</pattern>
        </encoder>
    </appender>
    <! -- Set log file -->
    <appender name="fileLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${LOG_PATTERN_FILE}</pattern>
        </encoder>
		<! -- Total file log -->
        <file>${LOG_PATH}/${LOG_FILE}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <! -- Daily file log -->
            <fileNamePattern>${LOG_PATH}/${LOG_FILE}-%d{yyyy-MM-dd}.log</fileNamePattern>
            <! -- Save log files for 7 days, delete them automatically after 7 days -->
            <maxHistory>7</maxHistory>
        </rollingPolicy>
    </appender>

    <! -- Set the log level and the class to log -->
    <root level="INFO">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileLog" />
    </root>
</configuration>
Copy the code

After the project is started, mylog. log will be generated in D:\log\dev. If our system is running continuously, mylog. log and 7 recent log files will be generated in our file directory mylog-year – month – date.


The sample code for this article has been uploaded togithub, point astarSupport!

Spring Boot series tutorial directory

Spring-boot-route (I) Several ways for Controller to receive parameters

Spring-boot-route (2) Several methods of reading configuration files

Spring-boot-route (3) Upload multiple files

Spring-boot-route (4) Global exception processing

Spring-boot-route (5) Integrate Swagger to generate interface documents

Spring-boot-route (6) Integrate JApiDocs to generate interface documents

Spring-boot-route (7) Integrate jdbcTemplate operation database

Spring-boot-route (8) Integrating mybatis operation database

Spring-boot-route (9) Integrate JPA operation database

Spring-boot-route (10) Switching between multiple data sources

Spring-boot-route (11) Encrypting database configuration information

Spring-boot-route (12) Integrate REDis as cache

Spring-boot-route RabbitMQ

Spring-boot-route Kafka

Spring-boot-route (15) Integrate RocketMQ

Spring-boot-route (16) Use logback to produce log files

Spring-boot-route (17) Use AOP to log operations

Spring-boot-route (18) Spring-boot-adtuator monitoring applications

Spring-boot-route (19) Spring-boot-admin Monitoring service

Spring-boot-route (20) Spring Task Implements simple scheduled tasks

Spring-boot-route (21) Quartz Implements dynamic scheduled tasks

Spring-boot-route (22) Enables email sending

Spring-boot-route (23) Developed wechat official accounts

Spring-boot-route (24) Distributed session consistency processing

Spring-boot-route (25) two lines of code to achieve internationalization

Spring-boot-route (26) Integrate webSocket

This series of articles are frequently used in the work of knowledge, after learning this series, to cope with daily development more than enough. If you want to know more, just scan the qr code below and let me know. I will further improve this series of articles!