1 引入jar包:maven resposity 中 选择logback classic module
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
2 添加logback.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>d:/logs/carmall-manager.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>d:/logs/carmall-manager.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- mapper 文件的包路径-->
<logger name="mapper" level="debug" />
<!--log4jdbc -->
<logger name="jdbc.sqltiming" level="debug"/>
<logger name="com.ibatis" level="debug" />
<logger name="com.ibatis.common.jdbc.SimpleDataSource" level="debug" />
<logger name="com.ibatis.common.jdbc.ScriptRunner" level="debug" />
<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate"
level="debug" />
<logger name="java.sql.Connection" level="debug" />
<logger name="java.sql.Statement" level="debug" />
<logger name="java.sql.PreparedStatement" level="debug" />
<logger name="java.sql.ResultSet" level="debug" />
<root level="info">
<appender-ref ref="console" />
<appender-ref ref="cn.iautos.manager" />
<appender-ref ref="com.ibatis" />
<appender-ref ref="rollingFile" />
</root>
</configuration>
最后输出日志
22:25:42,179 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [java.sql.PreparedStatement] to DEBUG 22:25:42,179 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [java.sql.ResultSet] to DEBUG 22:25:42,179 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO 22:25:42,179 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT] 22:25:42,180 |-ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an appender named [cn.iautos.manager]. Did you define it below instead of above in the configuration file? 22:25:42,180 |-ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - See http://logback.qos.ch/codes.html#appender_order for more details. 22:25:42,180 |-ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - Could not find an appender named [com.ibatis]. Did you define it below instead of above in the configuration file? 22:25:42,180 |-ERROR in ch.qos.logback.core.joran.action.AppenderRefAction - See http://logback.qos.ch/codes.html#appender_order for more details. 22:25:42,180 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [rollingFile] to Logger[ROOT] 22:25:42,180 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 22:25:42,181 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@7d417077 - Registering current configuration as safe fallback point 22:25:42.649 [main] DEBUG mapper.UserMapperI.getUser - ==> Preparing: select * from users where id=? 22:25:42.704 [main] DEBUG mapper.UserMapperI.getUser - ==> Parameters: 1(Integer) 22:25:42.742 [main] DEBUG mapper.UserMapperI.getUser - <== Total: 1 User{id=‘1‘, name=‘test‘, action=‘test‘}
最近在项目中添加日志
如果是spring boot项目的话:
只要在application.properties里面加上
logging.level.com.你的包名=true。就ok
slf4j + logback 输出日志:mybatis sql语句
原文:http://www.cnblogs.com/onfly/p/7748422.html