- 作者:柯基大佬
- 日期:2021-10-20
- 类型:Java
- 说明:本文源于 柯基 手写摘要,如需转载请带上链接或注明出处!
Log日志配置
均在application-dev.properties配置
一、springboot自带的
日志打印
# 执行sql打印开关 logging.level.com.a2j.mapper.*=debug
|
日志备份
# 存放的文件名 logging.file.name=logs/a2j.log
|
# 打印格式 logging.pattern.console=%d %-5level %logger : %msg%n logging.pattern.file=%d %-5level [%thread] %logger : %msg%n
|
二、springboot + Logback
dev和prod最大的区别就是,文件存放路径的配置
1.在resources下新建log/logback-dev.xml
2.在application-dev.properties中配置
# log打印开关 logging.level.com.a2j.mapper.*=debug # 日志配置 logging.config=classpath:log/logback-dev.xml
|
logback-dev.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <property name="PROJECT_NAME" value="a2j-java"/> <property name="LOG_HOME" value="/Users/apple/Desktop/Project/java_logs"/> <property name="MAX_HISTORY" value="365"/> <property name="MAX_FILESIZE" value="100MB"/> <property name="CHARSET" value="UTF-8"/>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/${PROJECT_NAME}/error.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}/%d{yyyy-MM-dd}/error.%i.zip</FileNamePattern> <MaxHistory>${MAX_HISTORY}</MaxHistory> <MaxFileSize>${MAX_FILESIZE}</MaxFileSize> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{50}] [%M] [%line] - %msg%n</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/${PROJECT_NAME}/info.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}/%d{yyyy-MM-dd}/info.%i.zip</FileNamePattern> <MaxHistory>${MAX_HISTORY}</MaxHistory> <MaxFileSize>${MAX_FILESIZE}</MaxFileSize> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{50}] [%M] [%line] - %msg%n</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/${PROJECT_NAME}/sql.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}/%d{yyyy-MM-dd}/sql.%i.zip</FileNamePattern> <MaxHistory>${MAX_HISTORY}</MaxHistory> <MaxFileSize>${MAX_FILESIZE}</MaxFileSize> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{50}] [%M] [%line] - %msg%n</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<logger name="error" additivity="true"> <appender-ref ref="ERROR_FILE"/> </logger>
<logger name="info" additivity="true"> <appender-ref ref="INFO_FILE"/> </logger>
<logger name="com.a2j.mapper.*" additivity="true"> <appender-ref ref="SQL_FILE"/> </logger>
<root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="ERROR_FILE"/> <appender-ref ref="INFO_FILE"/> <appender-ref ref="SQL_FILE"/> </root> </configuration>
|
1.在resources下新建log/logback-prod.xml
2.在application-dev.properties中配置
logback-prod.xml如下:
# log打印开关 logging.level.com.a2j.mapper.*=debug # 日志配置 logging.config=classpath:log/logback-dev.xml
|
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <property name="PROJECT_NAME" value="a2j-java"/> <property name="LOG_HOME" value="/usr/local/a2j-logs"/> <property name="MAX_HISTORY" value="365"/> <property name="MAX_FILESIZE" value="100MB"/> <property name="CHARSET" value="UTF-8"/>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/> <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/> <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${CONSOLE_LOG_PATTERN}</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/${PROJECT_NAME}/error.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}/%d{yyyy-MM-dd}/error.%i.zip</FileNamePattern> <MaxHistory>${MAX_HISTORY}</MaxHistory> <MaxFileSize>${MAX_FILESIZE}</MaxFileSize> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{50}] [%M] [%line] - %msg%n</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/${PROJECT_NAME}/info.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}/%d{yyyy-MM-dd}/info.%i.zip</FileNamePattern> <MaxHistory>${MAX_HISTORY}</MaxHistory> <MaxFileSize>${MAX_FILESIZE}</MaxFileSize> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{50}] [%M] [%line] - %msg%n</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/${PROJECT_NAME}/sql.log</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <FileNamePattern>${LOG_HOME}/${PROJECT_NAME}/%d{yyyy-MM-dd}/sql.%i.zip</FileNamePattern> <MaxHistory>${MAX_HISTORY}</MaxHistory> <MaxFileSize>${MAX_FILESIZE}</MaxFileSize> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%thread] [%logger{50}] [%M] [%line] - %msg%n</pattern> <charset>${CHARSET}</charset> </encoder> </appender>
<logger name="error" additivity="true"> <appender-ref ref="ERROR_FILE"/> </logger>
<logger name="info" additivity="true"> <appender-ref ref="INFO_FILE"/> </logger>
<logger name="com.a2j.*.mapper.*" additivity="true"> <appender-ref ref="SQL_FILE"/> </logger>
<root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="ERROR_FILE"/> <appender-ref ref="INFO_FILE"/> <appender-ref ref="SQL_FILE"/> </root> </configuration>
|