将SQL语句、参数、结果输出到控制台和DEBUG日志文件

1.依赖


<dependency>
    <groupId>org.codehaus.janinogroupId>
    <artifactId>janinoartifactId>
    <version>3.1.2version>
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.日志配置


<configuration scan="true" scanPeriod="60 seconds" debug="false">
    
    <property name="log.path" value="logs/finance-system"/>

    
    <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.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log
            fileNamePattern>
            
            <maxFileSize>100MBmaxFileSize>
            <maxHistory>60maxHistory>
            <totalSizeCap>20GBtotalSizeCap>
        rollingPolicy>
        
        <append>trueappend>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>INFOlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="file_debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/debug.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log
            fileNamePattern>
            
            <maxFileSize>100MBmaxFileSize>
            <maxHistory>60maxHistory>
            <totalSizeCap>20GBtotalSizeCap>
        rollingPolicy>
        
        <append>trueappend>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
            <evaluator>
                
                <expression>expression>
            evaluator>
            <OnMatch>ACCEPT OnMatch>
            <OnMismatch>DENYOnMismatch>
        filter>
    appender>

    <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.path}/error.logfile>
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            
            <fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log
            fileNamePattern>
            
            <maxFileSize>100MBmaxFileSize>
            <maxHistory>60maxHistory>
            <totalSizeCap>20GBtotalSizeCap>
        rollingPolicy>
        
        <append>trueappend>
        <encoder>
            <pattern>${log.pattern}pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
            
            <onMatch>ACCEPTonMatch>
            
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <logger name="com.finance" level="debug"/>

    
    <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

你可能感兴趣的:(java,笔记,mybatis,mybatis-plus,logback,debug,trace,sql)