SpringBoot之logback-spring.xml详细配置

《logback官网》
SpringBoot之logback-spring.xml详细配置_第1张图片
SpringBoot之logback-spring.xml详细配置_第2张图片
各种指导文件,有空自己去看,比如:我们需要调整的是布局,直接看Layouts。
SpringBoot之logback-spring.xml详细配置_第3张图片

SpringBoot之logback-spring.xml详细配置_第4张图片
pom.xml

	
    <profiles>
        <profile>
            <id>devid>
            <properties>
                <spring.profiles.active>devspring.profiles.active>
            properties>
            <activation>
                
                <activeByDefault>trueactiveByDefault>
            activation>
        profile>

        <profile>
            <id>testid>
            <properties>
                <spring.profiles.active>testspring.profiles.active>
            properties>
        profile>

        <profile>
            <id>prodid>
            <properties>
                <spring.profiles.active>prodspring.profiles.active>
            properties>
        profile>
    profiles>

需要注意的事项:

  1. SpringBoot默认的root级别是INFO,因此不需要DEBUG级别的日志,除非你想看SpringBoot的某些详细内容或者执行过程。
  2. 虽然可以通过配置文件配置生成日志输出到文件,但是即使你区分了环境,使用了某个环境并且只输出到控制台不输出到文件,还是会生成对应的日志文件。(只配置了输出到控制台,则不会生成文件)
  3. %L是一个转换符号,用于输出调用日志记录器的日志语句所在的代码行号。当使用%L时,会动态获取当前日志语句的代码行号,并将其作为日志消息的一部分进行输出。然而,使用%L会对性能产生一定的影响,尤其是在频繁记录大量日志消息的情况下。这是因为获取代码行号需要进行一些额外的操作,涉及到Java中的反射机制和堆栈跟踪的解析,这些操作会带来额外的开销。具体的性能影响取决于系统的复杂性、日志记录的频率和日志消息的大小。在性能敏感的生产环境中,建议在配置中避免使用%L,以减少不必要的性能损耗。你可以通过设置日志级别、打印线程名称等其他调试信息来辅助定位日志输出位置,而不必使用%L获取行号。总结起来,使用%L可以方便地输出代码行号,但会对性能产生一定的影响。在生产环境中,尽量避免频繁使用%L,以避免不必要的性能损耗。

简单配置






<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <contextName>logbackcontextName>
    
    <property name="log.path" value="./log" />

    
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>

    
    <property name="CONSOLE_LOG_PATTERN" value="%clr([%d{HH:mm:ss}){faint} %clr(%p) %clr(%.10t]){faint} %clr(%C{39}){cyan} %clr(%M:%L){magenta}: %m%n%wEx"/>
    
    <property name="FILE_LOG_PATTERN" value="[%d{MM-dd HH:mm:ss} %p %.10t] %C{39}\.%M\\(\\): %m%n"/>

    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>debuglevel>
        filter>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
            
            <charset>UTF-8charset>
        encoder>
    appender>

    
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/log_info.logfile>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
            <charset>UTF-8charset>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <maxFileSize>10MBmaxFileSize>
            <maxHistory>30maxHistory>
            <totalSizeCap>50GBtotalSizeCap>
        rollingPolicy>
        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>infolevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/log_warn.logfile>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
            <charset>UTF-8charset>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <maxFileSize>100MBmaxFileSize>
            <maxHistory>30maxHistory>
            <totalSizeCap>20GBtotalSizeCap>
        rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>warnlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>


    
    <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/log_error.logfile>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}pattern>
            <charset>UTF-8charset>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.logfileNamePattern>
            <maxFileSize>10MBmaxFileSize>
            <maxHistory>30maxHistory>
            <totalSizeCap>50GBtotalSizeCap>
        rollingPolicy>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    
    
    <springProfile name="dev">
        <root level="INFO">
            <appender-ref ref="STDOUT" />
        root>
    springProfile>

    
    <springProfile name="prod">
        <root level="ERROR">
            <appender-ref ref="ERROR_FILE" />
        root>
    springProfile>

    
    <springProfile name="test">
        <root level="INFO">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="INFO_FILE" />
            <appender-ref ref="ERROR_FILE" />
            <appender-ref ref="WARN_FILE" />
        root>
    springProfile>

configuration>

多环境配置

把原来resources下的logback-spring.xml删除,新增logback文件夹和对应环境的logback xml配置文件
SpringBoot之logback-spring.xml详细配置_第5张图片

application.yml

spring:
  application:
    name: '@artifactId@'
  profiles:
    #环境
    active: '@spring.profiles.active@'

logging:
  config: classpath:logback/@[email protected]

开发环境

application-dev.yml

logging:
  level:
  	# 正式环境打印SQL日志(一般没什么问题不建议开启。)
    com.fu.easycode.mapper: debug

生产环境

application-prod.yml

#按需配置

测试环境

application-test.yml

logging:
  level:
  	# 正式环境打印SQL日志(一般没什么问题不建议开启。)
    com.fu.easycode.mapper: debug

resources/logback/dev.xml


<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    
    <property name="CONSOLE_LOG_PATTERN" value="%clr([%d{HH:mm:ss}){faint} %clr(%p) %clr(%.10t]){faint} %clr(%C{39}){cyan} %clr(%M:%L){magenta}: %m%n%wEx"/>

    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder" charset="UTF-8">
            
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
        encoder>
    appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    root>

configuration>

resources/logback/prod.xml


<configuration scan="true" scanPeriod="60 seconds" debug="false">
    
    <springProperty scope="context" name="springApplicationName" source="spring.application.name" defaultValue="spring"/>
    
    <springProperty scope="context" name="springProfilesActive" source="spring.profiles.active" defaultValue="prod"/>
    
    <springProperty scope="context" name="cleanHistoryOnStart" source="logging.logback.rollingpolicy.clean-history-on-start" defaultValue="false"/>
    
    <springProperty scope="context" name="maxFileSize" source="logging.logback.rollingpolicy.max-file-size" defaultValue="10MB"/>
    
    <springProperty scope="context" name="totalSizeCap" source="logging.logback.rollingpolicy.total-size-cap" defaultValue="50GB"/>
    
    <springProperty scope="context" name="maxHistory" source="logging.logback.rollingpolicy.max-history" defaultValue="30"/>
    
    <springProperty scope="context" name="path" source="logging.file.path" defaultValue="./log/${springApplicationName}/${springProfilesActive}"/>

    <contextName>${springApplicationName}contextName>

    <property name="PATTERN_FILE" value="[%d{MM-dd HH:mm:ss} %p %.10t] %C{39}\.%M\\(\\): %m%n"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${path}/%d{yyyy-MM-dd}/%i.logfileNamePattern>
            <totalSizeCap>${totalSizeCap}totalSizeCap>
            <maxHistory>${maxHistory}maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${maxFileSize}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <cleanHistoryOnStart>${cleanHistoryOnStart}cleanHistoryOnStart>
        rollingPolicy>
        <encoder>
            <pattern>${PATTERN_FILE}pattern>
        encoder>
    appender>

    <root level="ERROR">
        <appender-ref ref="FILE" />
    root>

configuration>


resources/logback/test.xml


<configuration scan="true" scanPeriod="60 seconds" debug="false">
    
    <springProperty scope="context" name="springApplicationName" source="spring.application.name" defaultValue="spring"/>
    
    <springProperty scope="context" name="springProfilesActive" source="spring.profiles.active" defaultValue="prod"/>
    
    <springProperty scope="context" name="cleanHistoryOnStart" source="logging.logback.rollingpolicy.clean-history-on-start" defaultValue="false"/>
    
    <springProperty scope="context" name="maxFileSize" source="logging.logback.rollingpolicy.max-file-size" defaultValue="10MB"/>
    
    <springProperty scope="context" name="totalSizeCap" source="logging.logback.rollingpolicy.total-size-cap" defaultValue="50GB"/>
    
    <springProperty scope="context" name="maxHistory" source="logging.logback.rollingpolicy.max-history" defaultValue="30"/>
    
    <springProperty scope="context" name="path" source="logging.file.path" defaultValue="./log/${springApplicationName}/${springProfilesActive}"/>

    <contextName>${springApplicationName}contextName>

    
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>

    
    <property name="CONSOLE_LOG_PATTERN" value="%clr([%d{HH:mm:ss}){faint} %clr(%p) %clr(%.10t]){faint} %clr(%C{39}){cyan} %clr(%M:%L){magenta}: %m%n%wEx"/>
    
    <property name="PATTERN_FILE" value="[%d{MM-dd HH:mm:ss} %p %.10t] %C{39}\.%M\\(\\): %m%n"/>

    
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder" charset="UTF-8">
            
            <pattern>${CONSOLE_LOG_PATTERN}pattern>
        encoder>
    appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${path}/%d{yyyy-MM-dd}/%i.logfileNamePattern>
            <totalSizeCap>${totalSizeCap}totalSizeCap>
            <maxHistory>${maxHistory}maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${maxFileSize}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
            
            <cleanHistoryOnStart>${cleanHistoryOnStart}cleanHistoryOnStart>
        rollingPolicy>
        <encoder>
            <pattern>${PATTERN_FILE}pattern>
        encoder>
    appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    root>

configuration>

总结

建议用第二种方式,虽然麻烦点,但是对不同环境,做了不同的区分。方便阅读。因为springProperty可以和application的配置一起使用,再application里配置了,就可以覆盖掉xml里的默认配置。

彩色日志格式

<property name="CONSOLE_LOG_PATTERN" value="[%yellow(%d{HH:mm:ss}) %clr(%p) %boldBlue(%.10t)] %clr(%C{39}){cyan} %boldMagenta(%M): %m%n%wEx"/>

SpringBoot之logback-spring.xml详细配置_第6张图片

你可能感兴趣的:(#,Spring,Boot,spring,boot,spring,logback)