1.依赖
<!-- Logback 条件判断 -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.2</version>
</dependency>
2.系统配置
# 日志级别
logging:
level:
com:
baomidou:
mybatisplus: TRACE
# mybatis配置
mybatis:
# 搜索指定包别名
typeAliasesPackage: com.finance.system
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath:mapper/**/*.xml
# mybatis-plus配置
mybatis-plus:
# 搜索指定包别名
typeAliasesPackage: com.finance.system
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath:mapper/**/*.xml
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
3.日志配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<property name="log.path" value="logs/finance-system"/>
<!-- 格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
<property name="log.pattern" value="%d{yyyy/MM/dd-HH:mm:ss.SSS} %level [%thread] %caller{1} - %msg%n"/>
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/info.log</file>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 归档的日志文件的路径,例如今天是2013-12-21日志,当前写的日志文件路径为file节点指定,可以将此文件与file指定文件路径设置为不同路径,从而将当前日志文件或归档日志文件置不同的目录。
而2013-12-21的日志文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 -->
<fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<!-- each file should be at most 100MB, keep 60 days worth of history,
but at most 20GB -->
<maxFileSize>100MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统日志输出 -->
<appender name="file_debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/debug.log</file>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 归档的日志文件的路径,例如今天是2013-12-21日志,当前写的日志文件路径为file节点指定,可以将此文件与file指定文件路径设置为不同路径,从而将当前日志文件或归档日志文件置不同的目录。
而2013-12-21的日志文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 -->
<fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<!-- each file should be at most 100MB, keep 60 days worth of history,
but at most 20GB -->
<maxFileSize>100MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<!-- DEBUG=10000 TRACE=5000 -->
<expression><![CDATA[ return (level==10000 || level==5000); ]]></expression>
</evaluator>
<OnMatch>ACCEPT </OnMatch>
<OnMismatch>DENY</OnMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 归档的日志文件的路径,例如今天是2013-12-21日志,当前写的日志文件路径为file节点指定,可以将此文件与file指定文件路径设置为不同路径,从而将当前日志文件或归档日志文件置不同的目录。
而2013-12-21的日志文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 -->
<fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<!-- each file should be at most 100MB, keep 60 days worth of history,
but at most 20GB -->
<maxFileSize>100MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<!-- 追加方式记录日志 -->
<append>true</append>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.finance" level="debug"/>
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="WARN"/>
<logger name="com.finance.system.mapper" level="TRACE" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file_debug" />
</logger>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file_info"/>
<appender-ref ref="file_error"/>
</root>
</configuration>
4.效果展示
debug.log
2024/01/08-10:07:10.266 DEBUG [main] Caller+0 at org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:137)
- ==> Preparing: select config_id, config_name, config_key, config_value, config_type, create_by, create_time, update_by, update_time, remark from sys_config
2024/01/08-10:07:10.496 DEBUG [main] Caller+0 at org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:137)
- ==> Parameters:
2024/01/08-10:07:10.519 TRACE [main] Caller+0 at org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:143)
- <== Columns: config_id, config_name, config_key, config_value, config_type, create_by, create_time, update_by, update_time, remark
2024/01/08-10:07:10.521 TRACE [main] Caller+0 at org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:143)
- <== Row: 1, 主框架页-默认皮肤样式名称, sys.index.skinName, skin-blue, Y, admin, 2018-03-16 11:33:00, ry, 2018-03-16 11:33:00, 蓝色 skin-blue、绿色 skin-green、紫色 skin-purple、红色 skin-red、黄色 skin-yellow
2024/01/08-10:07:10.523 TRACE [main] Caller+0 at org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:143)
- <== Row: 2, 用户管理-账号初始密码, sys.user.initPassword, 123456, Y, admin, 2018-03-16 11:33:00, ry, 2018-03-16 11:33:00, 初始化密码 123456
2024/01/08-10:07:10.524 TRACE [main] Caller+0 at org.apache.ibatis.logging.jdbc.BaseJdbcLogger.trace(BaseJdbcLogger.java:143)
- <== Row: 3, 主框架页-侧边栏主题, sys.index.sideTheme, theme-dark, Y, admin, 2018-03-16 11:33:00, ry, 2018-03-16 11:33:00, 深色主题theme-dark,浅色主题theme-light
2024/01/08-10:07:10.525 DEBUG [main] Caller+0 at org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:137)
- <== Total: 3