Mybatis用log4j输出日志,输出sql到控制台
log4jar包+log4j.properties百度云下载地址永久有效: https://pan.baidu.com/s/15WZ-lcBPgFa-a6lkTVVvYw
1 导入jar包(直接复制jar包到lib目录下,然后build path),然后新建log4j.properties文件: 百度云中有log4j的书写规范教程。
log4j.rootLogger=INFO, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. #log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n # Print the date in ISO 8601 format log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n # Print only messages of level WARN or above in the package com.foo. log4j.logger.com.imooc=INFO
2 创建工具类
public class Log { public static Logger logger = Logger.getLogger(Log.class.getName()); }
3 直接调用即可。
比如:
1
2
3
4
|
catch (Exception e){ Log.logger.error(e.getMessage()); e.printStackTrace(); } |
Log.logger.info(“你传入的参数”);
如果想在控制台打出sql语句或者一些数据库信息:要在mybatis.xml中配置:
<settings> <setting name="logImpl" value="STDOUT_LOGGING"/> <settings>