首先,引入pom依赖:
接着修改application.yml配置文件(具体视情况而定)
driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:mysql://127.0.0.1:3306/xxxx?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
指定log4j2的配置文件路径
log4j2-test.xml的内容如下
<!-- 日志文件目录、压缩文件目录、日志格式配置 -->
<properties>
<Property name="fileName">c:/logs/xxxx/api</Property>
<Property name="fileGz">c:/logs/xxxx/api/7z</Property>
<Property name="LOG\_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} \[%thread\] %-5level %logger{36} - %msg%n</Property>
</properties>
<Appenders>
<!-- 输出控制台日志的配置 -->
<Console name="console" target="SYSTEM\_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<!-- 输出日志的格式 -->
<PatternLayout pattern="${LOG\_PATTERN}"/>
</Console>
<!-- 打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
<RollingRandomAccessFile name="infoFile" fileName="${fileName}/web-info.log" immediateFlush="false" filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-info.gz">
<PatternLayout pattern="${LOG\_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
<Filters>
<!-- 只记录info和warn级别信息 -->
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 -->
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>
<!-- 存储所有error信息 -->
<RollingRandomAccessFile name="errorFile" fileName="${fileName}/web-error.log" immediateFlush="false" filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-error.gz">
<PatternLayout pattern="${LOG\_PATTERN}"/>
<Policies>
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<Filters>
<!-- 只记录error级别信息 -->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 -->
<DefaultRolloverStrategy max="50"/>
</RollingRandomAccessFile>
<File name="log4jdbc\_delete" fileName="c:/logs/xxxx/api/log4jdbc\_delete.log">
<MarkerFilter marker="LOG4JDBC\_DELETE" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %-5p %t (%c:%L) - %m%n"/>
</File>
<File name="log4jdbc\_insert" fileName="c:/logs/xxxx/api/log4jdbc\_insert.log">
<MarkerFilter marker="LOG4JDBC\_INSERT" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %-5p %t (%c:%L) - %m%n"/>
</File>
<File name="log4jdbc\_update" fileName="c:/logs/xxxx/api/log4jdbc\_update.log">
<MarkerFilter marker="LOG4JDBC\_UPDATE" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %-5p %t (%c:%L) - %m%n"/>
</File>
<File name="log4jdbc\_select" fileName="c:/logs/xxxx/api/log4jdbc\_select.log">
<MarkerFilter marker="LOG4JDBC\_SELECT" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %-5p %t (%c:%L) - %m%n"/>
</File>
</Appenders>
<Loggers>
<logger name="org.springframework.aop.framework.CglibAopProxy" level="ERROR" additivity="false"/>
<logger name="springfox.documentation" level="ERROR" additivity="false"/>
<logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="ERROR" additivity="false"/>
<logger name="org.springframework.core.env.PropertySourcesPropertyResolver" level="ERROR" additivity="false"/>
<logger name="org.springframework.web.servlet.DispatcherServlet" level="ERROR" additivity="false"/>
<logger name="io.undertow.servlet" level="ERROR" additivity="false"/>
<logger name="org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod" level="TRACE"/>
<logger name="jdbc.sqlonly" level="INFO" additivity="false"/>
<logger name="jdbc.resultset" level="INFO" additivity="false"/>
<logger name="jdbc.connection" level="INFO" additivity="false"/>
<logger name="jdbc.audit" level="INFO" additivity="false"/>
<logger name="jdbc.sqltiming" level="INFO" additivity="false">
<appender-ref ref="console"/>
</logger>
<!--项目日志等级-->
<logger name="com.junko" level="DEBUG"/>
<logger name="log4jdbc.debug" level="INFO" additivity="false">
<appender-ref ref="console"/>
</logger>
<!-- 同步异步混合的方式进行写日志 -->
<AsyncLogger name="AsyncLogger" level="INFO" includeLocation="true">
<appender-ref ref="console" />
<appender-ref ref="infoFile" />
<appender-ref ref="errorFile" />
</AsyncLogger>
<asyncRoot level="INFO" includeLocation="true">
<appender-ref ref="console" />
<appender-ref ref="infoFile" />
<appender-ref ref="errorFile" />
</asyncRoot>
<logger name="log4jdbc.log4j2" level="info" additivity="false">
<MarkerFilter marker="LOG4JDBC\_OTHER" onMatch="DENY" onMismatch="NEUTRAL"/>
<appender-ref ref="log4jdbc\_insert"/>
<appender-ref ref="log4jdbc\_delete"/>
<appender-ref ref="log4jdbc\_update"/>
<appender-ref ref="log4jdbc\_select"/>
<appender-ref ref="console"/>
</logger>
</Loggers>
项目运行后,打开目录查看:
增删改查的执行语句已分开保存,再查看log4jdbc_select.log文件:
可以看到,sql语句里的 ? 占位符已经是实际传入的参数了。