《logback官网》
各种指导文件,有空自己去看,比如:我们需要调整的是布局,直接看Layouts。
<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>
需要注意的事项:
<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配置文件
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
<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>
<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>
<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"/>