Spring Boot log configuration (ii)

Source: Internet
Author: User
Tags java util log4j

Support Log Framework : Java Util Logging , Log4J2 and Logback , default is to use logback
How to configure : Default profile configuration and reference external configuration file configuration

1. Default configuration file Configuration
    • Not recommended: Not flexible enough, not log4j2 friendly

      # 日志文件名,比如:roncoo.log,或者是 /var/log/roncoo.loglogging.file=roncoo.log # 日志级别配置,比如: logging.level.org.springframework=DEBUGlogging.level.*=infologging.level.org.springframework=DEBUG
2, Configuration Logback
  • referencing external configuration files

    Spring boot is loaded by default classpath:logback-spring.xml or classpath:logback-spring.groovy .
    Use a custom configuration file that is configured as follows: logging.config=classpath:logback-roncoo.xml Note: Do not use Logback to name it, otherwise spring boot will not be fully instantiated.

    ① using spring boot-based configuration

    <?xml version="1.0" encoding="UTF-8"?><configuration><include resource="org/springframework/boot/logging/logback/base.xml"/><logger name="org.springframework.web" level="DEBUG"/></configuration>

    ② Custom Configuration

    <?xml version= "1.0" encoding= "UTF-8"?><configuration> <!--file output format--<property name= "PATTERN" Value= "%-12 (%d{yyyy-mm-dd HH:mm:ss. SSS}) |-%-5level [%thread]%c [%l]-| %msg%n "/> <!--TEST file path--<property name=" Test_file_path "value=" Logs/info "/> <!--Pro file path --<property name= "Pro_file_path" value= "Logs/warn"/> <!--development environment--<springprofile name= "Dev"                > <appender name= "CONSOLE" class= "Ch.qos.logback.core.ConsoleAppender" > <encoder> <pattern>${PATTERN}</pattern> </encoder> </appender> <logger N Ame= "Com.roncoo.education" level= "Debug"/> <root level= "info" > <appender-ref ref= "CONSOLE"/ > </root> </springProfile> <!--test environment-<springprofile name= "Test" > &lt ;! --Generate one file per day--<appender name= "Test-file" class= "CH.QOS.LOgback.core.rolling.RollingFileAppender "> <!--file path--<file>${test_file_path}</f  ile> <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <!--                File name-<fileNamePattern>${TEST_FILE_PATH}.%d{yyyy-MM-dd}.log</fileNamePattern>            <!--file maximum saved history-<MaxHistory>100</MaxHistory> </rollingPolicy> <layout class= "Ch.qos.logback.classic.PatternLayout" > <pattern>${pattern}</pattern > </layout> </appender> <root level= "info" > <appender-ref ref= "Test-file"/> </root> </springProfile> <!--production Environment--<springprofile name= "prod" &G        T <appender name= "Prod_file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" > <file>${pro_fil E_path}</file> <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" > <file Namepattern>${pro_file_path}.%d{yyyy-mm-dd}.log</filenamepattern> <maxhistory>100</maxhis                tory> </rollingPolicy> <layout class= "Ch.qos.logback.classic.PatternLayout" > <pattern>${PATTERN}</pattern> </layout> </appender> <root level = "Warn" > <appender-ref ref= "prod_file"/> </root> </springprofile></configura Tion>
3, Configuration log4j
  • referencing external configuration files

    Remove Logback Dependency package, add log4j2 dependency Package

    <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>    <exclusions>        <exclusion>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-logging</artifactId>        </exclusion>    </exclusions></dependency><!-- 使用log4j2 --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-log4j2</artifactId></dependency>

    ① in classpath add log4j2.xml or log4j2-spring.xml (Spring boot default load)

    ② a custom configuration file

    <?xml version="1.0" encoding="utf-8"?><configuration>    <properties>        <!-- 文件输出格式 -->        <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property>    </properties>    <appenders>        <Console name="CONSOLE" target="system_out">            <PatternLayout pattern="${PATTERN}" />        </Console>    </appenders>    <loggers>        <logger name="com.roncoo.education" level="debug" />        <root level="info">            <appenderref ref="CONSOLE" />        </root>    </loggers></configuration>
4. Comparison
    • Performance comparison: Log4J2 and Logback both superior log4j (not recommended)
    • How to configure: Logback the most concise, spring boot default, recommended to use
5, Configuration logging
    • Reference documents
6. List of acknowledgements
    • Feng Yongwei

Spring Boot log configuration (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.