Spring Boot Tutorial (5): Default log logback

1. Brief introduction

Spring Boot's internal logging system uses Commons Logging, but the underlying logging implementation is open. The default is to provide configuration for Java Util Logging, Log4J, Log4J2 and Logback. Each case is preconfigured to use console output, and optional file output is also available.

By default, if you use 'Starter POMs', then Logback will be used for logging. To ensure that dependencies that use Java Util Logging, Commons Logging, Log4J or SLF4J work correctly, the correct Logback routing is also included.

official documentation

Spring Boot integration logback needs to add spring-boot-starter-loggingdependencies, and this dependency has been spring-boot-starteradded in, so there is no need to add this dependency (as shown below)

write picture description here

By default, Spring Boot will use Logback to log and output to the console with INFO level.
write picture description here

2. Log format

Official log output example

2014-03-05 10:57:51.112  INFO 45469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698  INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-03-05 10:57:51.702  INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]

The output content elements are as follows:

  • Time Date: Millisecond accurate for easy sorting.
  • Log level: ERROR, WARN, INFO, DEBUG or TRACE
  • Process ID
  • Separator: — Identifies the start of the actual log
  • Thread name: in square brackets (may truncate console output)
  • Logger name: usually use the class name of the source code
  • log content

Note: Logback has no FATALlevels. It is mapped to ERRORlevels.

3. File output

official documentation

The default configuration of Spring Boot will only be output to the console and will not be recorded in a file, but we usually need to record it in a file when using it in a production environment. To increase file output, you need to configure logging.file or logging.path properties in application.properties.

The following table will illustrate the rules when using the logging.fileand logging.pathtwo properties:

loggin.file logging.path Example describe
Not configured Not configured output to console only
specified file Not configured my.log Output to the specified log file. Can be a relative path or an absolute path
unspecified specified directory / var / log Output to the specified directory, the default log file name spring.logis a relative path or an absolute path

Note: logging.fileand logging.pathdo not use at the same time, if used at the same time will only logging.filetake effect.

By default, when the size of the log file reaches 10MB, it will be split once to generate a new log file. If you want to modify the size, you can logging.file.max-sizeconfigure it through.
If you want to limit the number of log files, you can logging.file.max-historyset it.

Fourth, multi-environment log configuration

The log levels of the development environment and the production environment are generally different. In order to obtain more log information during development, the debug level is generally set, while the production environment is generally set to the info or error level to improve efficiency. For these different log configurations, you only need to put them in different files, and put the same configuration in application.properties. For details, please refer to
Spring Boot Tutorial (4): Multi-Environment Configuration

Five, custom log configuration

official documentation

Since the logging service is generally initialized before the ApplicationContext is created, it does not have to be controlled by Spring's configuration file. Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.

Depending on the logging system, you can organize the configuration file names according to the following rules, and they will be loaded correctly:

log rule
logback logback-spring.xml, logback-spring.groovy, logback.xml, orlogback.groovy
log4j log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml
log4j2 log4j2-spring.xmlorlog4j2.xml
JDK (Java Util Logging) logging.properties

SpringBoot officially recommends that you use -springthe filename for log configuration (for example, logback-spring.xml instead of logback.xml)

Custom configuration file reference:
logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration  scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>demo</contextName>
    <property name="log.path" value="/Users/mengday/Documents/logs" />
    <property name="log.name" value="demo" />

    <!--输出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
             <level>ERROR</level>
         </filter>-->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!--输出到文件-->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/${log.name}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>1MB</maxFileSize>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </root>

    <logger name="com.example.demo.mapper" level="DEBUG" additivity="false">
        <appender-ref ref="console" />
    </logger>

    <!-- 测试环境+开发环境. -->
    <springProfile name="test,dev">
        <logger name="com.example.demo" level="INFO" />
    </springProfile>
    <!-- 生产环境 -->
    <springProfile name="prod">
        <logger name="com.example.demo" level="ERROR"/>
    </springProfile>
</configuration>





refer to:

https://blog.csdn.net/vbirdbest/article/details/79669545

https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#boot-features-logging

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326683849&siteId=291194637