之前客户端程序由于Websockt包依赖的log4j,就用log4j写日志了,Web用的log4j2没毛病。用log4j的多个logger的日志都写到一个文件里了,查了很多资料都没解决。今天闲了解决一下。
最后好使的配置
# 设置日志根
log4j.rootLogger = INFO,Exception,Debug
# additivity的作用在于 children-logger是否使用 rootLogger的配置,默认是开启true
log4j.addivity.org.apache=false
# 输出到日志文件
log4j.logger.Exception=INFO,Exception
log4j.additivity.Exception = false
log4j.appender.Exception.encoding=UTF-8
log4j.appender.Exception = org.apache.log4j.DailyRollingFileAppender
log4j.appender.Exception.File = logs/Exception.log
log4j.appender.Exception.Append = true
log4j.appender.Exception.Threshold = INFO
log4j.appender.Exception.layout = org.apache.log4j.PatternLayout
log4j.appender.Exception.ImmediateFlush=true
log4j.appender.Exception.BufferedIO=false
log4j.appender.Exception.BufferSize=0
log4j.appender.Exception.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] %m%nlog4j.logger.Debug=INFO,Debug
log4j.additivity.Debug = false
log4j.appender.Debug.encoding=UTF-8
log4j.appender.Debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.Debug.File = logs/Debug.log
log4j.appender.Debug.Append = true
log4j.appender.Debug.Threshold = INFO
log4j.appender.Debug.layout = org.apache.log4j.PatternLayout
log4j.appender.Debug.ImmediateFlush=true
log4j.appender.Debug.BufferedIO=false
log4j.appender.Debug.BufferSize=0
log4j.appender.Debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] %m%n
关键点是logger第一位是日志级别
各种查资料和问了人工智能,那东西问常规的还行,稍微偏一点的问题就跟人工智障一样
log4j2采用滚动日志配置和限制每个日志文件的大小
写日志工具类
package Monitor.Util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;/*** 写日志工具类*/
public class LogUtils {//调试日志static final Logger loggerDebug = LoggerFactory.getLogger("Debug");//异常日志static final Logger exceptionDebug = LoggerFactory.getLogger("Exception");/*** 书写调试日志* @param message 日志内容*/public static void WriteDebugLog(String message){loggerDebug.info(message);}/*** 书写异常日志* @param message 描述* @param exception 异常*/public static void WriteExceptionLog(String message, Exception exception){exceptionDebug.error(message, exception);}}