使用Log4j与log4j2配置mybatisplus打印sql日志

环境:项目非完全spring项目,没有spring的配置文件。执行sql时老是不打印sql语句。因此进行修改,过程比较坎坷,记录一下。
我尝试使用log4j和log4j2进行配置 最终把这两种全部配置记录上
Log4j配置
如果项目用的是log4j需要进行配置打印sql的步骤

  1. 首先引入log4j的包
  2. 配置一下log4j的配置文件
#STDOUT 表示配置sql语句输出
log4j.rootLogger=ERROR,STDOUT
#xxx代表你项目中的mapper路径
log4j.logger.xxx.xxx.xxx=DEBUG

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.Target=System.out
#log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} %-5p %c{1.}:%L - %m%n

Log4j2配置
如果使用log4j2需要进行配置打印sql的步骤

  1. 引入log4j2包
  2. 配置log4j2配置文件
<Configuration status="WARN">
    <Appenders>
        <Console name="myConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{MM-dd HH:mm:ss} %-5p] [%t] %c{2\} - %m%n%ex"/>
        </Console>

        <Console name="myConsole2" target="STDOUT">
            <PatternLayout pattern="[%d{MM-dd HH:mm:ss} %-5p] [%t] %c{2\} - %m%n%ex"/>
        </Console>
        <RollingFile name="activexAppender" fileName="../log/jxedtgouchescf.log" filePattern="../log/jxedtgouchescf.log.%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <Pattern>[%d{MM-dd HH:mm:ss SSS} %-5level] [%t] %c{3} - %m%n%ex</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="myConsole"/>
            <AppenderRef ref="activexAppender"/>
        </Root>
        <!--这里的name为你自己mapper的地址-->
        <Logger name="xxx.xxx.mapper" level="DEBUG">
            <AppenderRef ref="myConsole"/>
        </Logger>
    </Loggers>
</Configuration>

ok这样就能打出来具体的执行sql了

[12-13 17:20:32 DEBUG] [main] cxxxx.xxxx.xxx.selectCount - ==>  Preparing: SELECT COUNT(1) FROM table WHERE (state = ?) 
[12-13 17:20:32 DEBUG] [main] cxxxx.xxxx.xxx.selectCount - ==>  Preparing: SELECT COUNT(1) FROM table WHERE (state = ?) 
[12-13 17:20:32 DEBUG] [main] cxxxx.xxxx.xxx.selectCount - ==> Parameters: 0(Integer)
[12-13 17:20:32 DEBUG] [main] cxxxx.xxxx.xxx.selectCount - ==> Parameters: 0(Integer)
[12-13 17:20:32 DEBUG] [main] cxxxx.xxxx.xxx.selectCount - <==      Total: 1
[12-13 17:20:32 DEBUG] [main] cxxxx.xxxx.xxx.selectCount - <==      Total: 1

这里顺便提一下mybatisplus开启打印日志的配置方法适用于spring-boot
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值