SpringBoot + logback achieved by business log output to a different file

    The company has a project that requires several third-party systems and docking. This project, the log must be recorded in detail, or thrown out of the question that a variety of pot. Although the project inside the docking system and third-party business records related to very detailed logs, but because the log of the entire project in a single file, more trouble when troubleshooting. So we are hoping to turn these log generation and third-party docking in another separate file. This is achieved by the title of the business to a different output log file, you can start to explain the specific implementation.

A program

    Due to the need to generate different log files by business, by business to distinguish see, my first impression is that business can actually be distinguished by package name. In fact, so long as we realize different packages following log output to a different file, you will be able to realize the demand. Because I've played log4j2, this is not difficult to achieve, so there was an immediate idea of.
Specific implementation ideas are as follows:
     (1) to output a custom appender file (understood as a log output device)
    (2) Configuration logger, logger need to generate the full name for the package that package file name alone, then reference appender defined above in which

Second, the specific implementation

(1) preparation stage

    Since the project uses SpringBoot framework, and using the default logging framework logback. Read Xiaguan network, only need to define a logback-spring.xml XML file in the following resources logback can override the default configuration.
    Because SpringBoot default logging configuration is very good, and therefore want the default configuration retained. Xml logback to find the default configuration by viewing springboot jar package.
! Its location is spring-boot-2.1.0.RELEASE.jar \ org \ springframework \ boot \ logging \ logback \ base.xml, this default xml in the following configurations:
    •  Defines two appender, are output to the console to appender and an output file appender
    •  The default log level is info, default cited the two appender defined above,

(2) the actual combat phase

    • Logback-spring.xml create a new file in the resources directory
    • The default configuration of logback ( base.xml ) to copy the contents of our xml file, because we want to keep its default configuration
    • A definition of output to a file appender
    • Define a Logger, Logger's name for the package that you need to generate a separate file of the full package name
    • logger which we defined above referenced appender
     Specifically described herein, the logger is referred to the local custom configuration, its priority is higher than the global configuration (refer to the root), local configuration is understood to cover the global configuration.
     logback-spring.xml content as follows:
<?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <!--官方配置 start-->
    <!--保留官方配置,方便使用官方配置的特性,参考:spring-boot-2.1.0.RELEASE.jar!\org\springframework\boot\logging\logback\base.xml-->
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="info">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
    <!--官方配置 end-->

    <!--######自定义配置  start########-->
    <!-- 自定义配置__单独输出到一个日志文件中 -->
    <appender name="Biz_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${LOG_FILE}_BIZ.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE}_BIZ.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
            <maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
            <maxHistory>${LOG_FILE_MAX_HISTORY:-0}</maxHistory>
        </rollingPolicy>
    </appender>

    <!--指定包或者类的日志配置(这里是局部配置,它能覆盖全局配置)-->
    <!-- 自定义配置__配置业务日志输出至单独的日志文件中 -->
    <logger name="com.jwx.digital.client.haier.http" additivity="false" level="debug">
        <!-- 引用自定义的appender -->
        <appender-ref ref="Biz_LOG"/>
        <!-- 这里也引用控制台appender,才能在控制台中看到我们的日志 -->
        <appender-ref ref="CONSOLE"/>
    </logger>
    
    <!--######自定义配置  end########-->

</configuration>

(3) the validation phase

    application.yml project made the following configuration log
# 日志打印
logging:
    file: /log/digital-client.log  #日志输出到这个文件
    After the project is run, we found a next digital-client.log of digital-client.log_BIZ.log file, and the file contents inside that is our third-party business butt log.

III Summary

    (1) In fact, to achieve this there are other options, but depending on the operating environment of the current project, and the structure of the code, which modify the way the log configuration is simple
    (2) Through this log configuration, learning the default xml configuration springboot log of logback

Guess you like

Origin www.cnblogs.com/zeng1994/p/f9bff238b13a0bf8fb8bf88c41db7a34.html