log4j:可以输出日志到控制台或文件。
一、导入包:log4j
二、新建src/log4j.properties文件:
- #set log level and output place
- log4j.rootLogger = debug,stdout,D
- log4j.appender.stdout = org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target = System.out
- log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
- log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
- log4j.appender.D.File = log.log
- log4j.appender.D.Append = true
- log4j.appender.D.Threshold = DEBUG
- log4j.appender.D.layout = org.apache.log4j.PatternLayout
- log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
三、使用log4j:
- Logger log=Logger.getLogger(Test.class);
- log.debug("this is a 测试");
四、在mybatis中开启log4j:
- <configuration>
- <settings>
- <setting name="logImpl" value="log4j"/>
- </settings>
五、log4j.propertie控制输出内容:
- log4j.rootLogger = error,stdout,D
- log4j.logger.com.mybatis=debug (只输出com.mybatis中的日志)