首先,引入pom依賴:mysql
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- Log4j2 異步支持 --> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.3.6</version> </dependency> <dependency> <groupId>org.bgee.log4jdbc-log4j2</groupId> <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId> <version>1.16</version> </dependency>
接着修改application.yml配置文件(具體視狀況而定)web
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的配置文件路徑spring
log4j2-test.xml的內容以下sql
<?xml version="1.0" encoding="UTF-8"?> <!-- Configuration後面的status,這個用於設置log4j2自身內部的信息輸出,能夠不設置,當設置成trace時, 你會看到log4j2內部各類詳細輸出。能夠設置成OFF(關閉) 或 Error(只輸出錯誤信息)。 30s 刷新此配置 --> <configuration status="WARN" monitorInterval="30"> <!-- 日誌文件目錄、壓縮文件目錄、日誌格式配置 --> <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> </configuration>
項目運行後,打開目錄查看:api
增刪改查的執行語句已分開保存,再查看log4jdbc_select.log文件:mvc
能夠看到,sql語句裏的 ? 佔位符已是實際傳入的參數了。app