myeclipse中有一个springmvc项目,集成了log4j的日志,每次看到控制台打出很多的info信息,会很烦,所以要设置输出的level级别,参考如下说明:
就是log4j的日志级别使用错误,导致mybatis信息打印不出来;日志级别每个Logger都被了一个日志级别(log level),用来控制日志信息的输出。日志级别从高到低分为:A:off 最高等级,用于关闭所有日志记录。B:fatal 指出每个严重的错误事件将会导致应用程序的退出。C:error 指出虽然发生错误事件,但仍然不影响系统的继续运行。D:warm 表明会出现潜在的错误情形。E:info 一般和在粗粒度级别上,强调应用程序的运行全程。F:debug 一般用于细粒度级别上,对调试应用程序非常有帮助。G:all 最低等级,用于打开所有日志记录。上面这些级别是定义在org.apache.log4j.Level类中。Log4j只建议使用4个级别,优先级从高到低分别是error,warn,info和debug。通过使用日志级别,可以控制应用程序中相应级别日志信息的输出。例如,如果使用b了info级别,则应用程序中所有低于info级别的日志信息(如debug)将不会被打印出来。根据上面的log4j的说明,另外根据mybatis的细粒度得出,mybatis要把log4j的日志至少配置到DEBUG级别,才能显示出sql日志,如下:.demo.user=DEBUG
我的工程里面的配置是这样的:
log4j.rootLogger=ERROR,Consolelog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=[%p] - TIME:[%d{yyyy-MM-dd HH:mm:ss.SSS}] - [%t] - CLASS:[%c] at [line:%L] - MSG:[%m]%n#log4j.additivity.loggerInfo=falselog4j.logger.loggerInfo=INFO,loggerInfolog4j.appender.loggerInfo = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.loggerInfo.DatePattern='.'yyyy-MM-ddlog4j.appender.loggerInfo.File = /var/log/RMSServer/info/info.loglog4j.appender.loggerInfo.Append = truelog4j.appender.loggerInfo.Threshold = INFO log4j.appender.loggerInfo.layout = org.apache.log4j.PatternLayoutlog4j.appender.loggerInfo.layout.ConversionPattern = TIME:[%d{yyyy-MM-dd HH:mm:ss.SSS}] - [%t] - %m %n#log4j.additivity.loggerError=falselog4j.logger.loggerError=INFO,loggerErrorlog4j.appender.loggerError = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.loggerError.DatePattern='.'yyyy-MM-ddlog4j.appender.loggerError.File = /var/log/RMSServer/error/error.loglog4j.appender.loggerError.Append = truelog4j.appender.loggerError.Threshold = ERROR log4j.appender.loggerError.layout = org.apache.log4j.PatternLayoutlog4j.appender.loggerError.layout.ConversionPattern = TIME:[%d{yyyy-MM-dd HH:mm:ss.SSS}] - [%t] - %m %n
控制台和盘符下面的日志信息对比如下:
log4jUtil.java代码如下:
package com.springMVC.util;import java.io.PrintWriter;import java.io.StringWriter;import org.apache.log4j.Level;import org.apache.log4j.LogManager;import org.apache.log4j.Logger;/*** 日志工具类* * @author chengjunwen*/public class LogUtil {private static Logger loggerInfo = Logger.getLogger("loggerInfo");private static Logger loggerError = Logger.getLogger("loggerError");public static void debug(String info) {StackTraceElement[] temp = Thread.currentThread().getStackTrace();StackTraceElement a = (StackTraceElement) temp[2];loggerInfo.debug(getLogLocality(a, info));}public static void info(String info) {//loggerInfo.setLevel(Level.ERROR);StackTraceElement[] temp = Thread.currentThread().getStackTrace();StackTraceElement a = (StackTraceElement) temp[2];loggerInfo.info(getLogLocality(a, info));}public static void warn(String info) {StackTraceElement[] temp = Thread.currentThread().getStackTrace();StackTraceElement a = (StackTraceElement) temp[2];loggerInfo.warn(getLogLocality(a, info));}public static void error(String info) {StackTraceElement[] temp = Thread.currentThread().getStackTrace();StackTraceElement a = (StackTraceElement) temp[2];loggerError.error(getLogLocality(a, info));}public static void error(Exception e) {StackTraceElement[] temp = Thread.currentThread().getStackTrace();StackTraceElement a = (StackTraceElement) temp[2];StringWriter sw = new StringWriter();e.printStackTrace(new PrintWriter(sw));loggerError.error(getLogLocality(a, sw.toString()));}public static String getLogLocality(StackTraceElement a, String msg) {StringBuffer sb = new StringBuffer();sb.append("CLASS:[");sb.append(a.getClassName());sb.append("] METHOD:[");sb.append(a.getMethodName());sb.append("] at [line:");sb.append(a.getLineNumber());sb.append("] - MSG:[");sb.append(" --- " + msg + " --- ");sb.append("]");return sb.toString();}}
如果觉得《springmvc log4j设置日志输出级别》对你有帮助,请点赞、收藏,并留下你的观点哦!