mybatis log4j2打印sql+日志实例代码
前言
将采用slf+log4j2方式进行打印日志,以下用两种方式,一种基于maven的springboot2,一种jdk6,普通jar包的springmvc
基于maven的springboot
第一步、由于前阵子java8可对应的log4j漏洞问题,公司要求log4j升级到2.17.1,因此需要把springboot自带的log4j进行排除引入高版本。
<properties>
<log4j2.version>2.17.1</log4j2.version>
</properties>
<!--log4j漏洞升级-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
第二步 log4j.xml
以下patternLayout做了对邮件、手机号、身份证号敏感数据转码**
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout>
<!-- <pattern>%d{ISO8601}|%level|%thread|%c{1.}|%L|%msg%n</pattern>-->
<pattern><![CDATA[%d{ISO8601}|%level|%thread|%c|%L|%replace{%replace{%replace{%msg}{(^|\D)([\d]{12})(\d{3}|\d{6}|\d{5}(\d|X|x))(\D|$)}{$1$2******$5}}{(^|\D)(1([358][0-9]|4[579]|66|7[0135678]|9[89])([0-9]{4})([0-9]{4}))(\D|$)}{$11$3****$5$6}}{(([A-Za-z0-9-_.]{1,3})([A-Za-z0-9-_.]*)(@[a-zA-Z0-9-_.]+(\.[a-zA-Z-_.]+)+))}{$2***$4}%n]]></pattern>
</PatternLayout>
</Console>
<RollingFile name="file" filePattern="/qhapp/applogs/imeeting/imeeting_business_%d{yyyyMMdd}_%i.log" immediateFlush="true">
<PatternLayout>
<pattern>%d{ISO8601}|%level|%thread|%c{1.}|%L|%msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="console"/>
</Root>
<Logger name="org.mybatis" level="INFO" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="org.springframework" level="INFO" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="com.company.booster" level="INFO" additivity="false">
<AppenderRef ref="console"/>
</Logger>
</Loggers>
</Configuration>
第三步 输出sql的mapper接口包,写在*.properties,放置在resource下
#debug log 自己项目的包,写dao接口的位置
logging.level.com.company.pm.mapper=debug
重新启动即可,sql日志就会出来,如果想自己logger出来,用slf即可
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("6867868@qq.com");
第二种 jdk6,普通jar包的springmvc
spring3 + mybatis3
jdk6建议log4j版本为2.3.2(官方因漏洞推荐),引入以下个jar包
log4j-slf4j-impl-2.3.2.jar、log4j-core-2.3.2.jar、log4j-api-2.3.2.jar、slf-api-1.7.25.jar、log4j-slf-impl-2.3.2.jar
由于不是maven项目,只是普通的mvc结构,因此我在src下建了两个文件log4j2.xml(有些是resource,这个项目前期没有这个,反正最后打包后在class下面就是了)
log4j2.xml,不知道,为啥在jdk6以及版本log4j2.3.2里面为何pattern不能写多个replace,因此我只排除了邮箱敏感信息
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout>
<pattern><![CDATA[%d{ISO8601}|%level|%thread|%c|%L|%replace{%msg}{(([A-Za-z0-9-_.]{1,3})([A-Za-z0-9-_.]*)(@[a-zA-Z0-9-_.]+(\.[a-zA-Z-_.]+)+))}{$2***$4}%n]]></pattern>
<!-- <pattern><![CDATA[%d{ISO8601}|%level|%thread|%c|%L|%replace{%replace{%replace{%msg}{(^|\D)([\d]{12})(\d{3}|\d{6}|\d{5}(\d|X|x))(\D|$)}{$1$2******$5}}{(^|\D)(1([358][0-9]|4[579]|66|7[0135678]|9[89])([0-9]{4})([0-9]{4}))(\D|$)}{$11$3****$5$6}}{(([A-Za-z0-9-_.]{1,3})([A-Za-z0-9-_.]*)(@[a-zA-Z0-9-_.]+(\.[a-zA-Z-_.]+)+))}{$2***$4}%n]]></pattern>-->
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="console"/>
</Root>
<Logger name="com.xxx自己的包.dao" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="org.mybatis" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="org.apache.ibatis" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="org.springframework" level="INFO" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<Logger name="com.xxx自己的包" level="INFO" additivity="false">
<AppenderRef ref="console"/>
</Logger>
</Loggers>
</Configuration>
mybatis-config.xml
<!-- 配置SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:configuration/mybatis-config.xml" />
.....
</bean>
application-context.cml
<!-- 配置SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:configuration/mybatis-config.xml" />
.....
</bean>
这里jdk6 slf+ log4j2有个坑点,我不知道为何log4j.properties与log4j.xml不能同时存在
测试类
Logger logger = LoggerFactory.getLogger(getClass());
logger.debug("6867868@qq.com");
总结
到此这篇关于mybatis log4j2打印sql日志的文章就介绍到这了,更多相关mybatis log4j2打印日志内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章