在工作中,排查问题时难免需要输出MyBatis真正执行的sql及参数,需查看sql信息,如下是一种配置方法:
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="info"> <Appenders> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1}(%F:%L) - %m%n" /> </Console> <RollingFile name="fixedTimeFileAppender" fileName="app/projectName.log" filePattern="app/projectName.log.%d{yyyy-MM-dd}.%i.log"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1}(%L) - %m%n" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="200 MB" /> </Policies> <DefaultRolloverStrategy max="24" /> </RollingFile> <RollingFile name="fixedTimeFilesqlAppender" fileName="app/projectName-sql.log" filePattern="app/projectName-sql.log.%d{yyyy-MM-dd}.%i.log"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1}(%L) - %m%n" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="200 MB" /> </Policies> <DefaultRolloverStrategy max="24" /> </RollingFile> <Async name="async" bufferSize="512"> <AppenderRef ref="fixedTimeFileAppender" /> </Async> <Async name="asyncsql" bufferSize="512" level="debug"> <AppenderRef ref="fixedTimeFilesqlAppender" /> </Async> </Appenders> <Loggers> <Logger name="cn.bijian" level="info" additivity="false"> <AppenderRef ref="async" /> </Logger> <Logger name="cn.bijian.projectName.dao" level="debug" additivity="false"> <AppenderRef ref="asyncsql" /> </Logger> <Root level="info"> <AppenderRef ref="async" /> </Root> </Loggers> </Configuration>