log4j 详细配置实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了log4j 详细配置实例相关的知识,希望对你有一定的参考价值。

一,maven引入jar包 org.slf4j slf4j-api 1.6.6 org.slf4j slf4j-log4j12 1.6.6 log4j log4j 1.2.17 # 二,详细配置: ### root日志 ### log4j.rootLogger = DEBUG, stdout, debug, info , warn, error #控制台日志 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= %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n ### 保存Debug信息到单独文件 ### log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender log4j.appender.debug.File = C:\\logs\\log4j\\debug.log log4j.appender.debug.Append = true ### 只输出DEBUG级别以上的日志!!! log4j.appender.debug.Threshold = DEBUG log4j.appender.debug.layout = org.apache.log4j.PatternLayout log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n log4j.appender.debug.DatePattern = ‘.‘yyyy-MM-dd-HH-mm ### 只输出DEBUG级别的日志!!! log4j.appender.debug.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter log4j.appender.debug.filter.infoFilter.LevelMin=DEBUG log4j.appender.debug.filter.infoFilter.LevelMax=DEBUG ### 保存Info信息到单独文件 ### log4j.appender.info = org.apache.log4j.DailyRollingFileAppender log4j.appender.info.File = C:\\logs\\log4j\\info.log log4j.appender.info.Append = true ### 只输出INFO级别以上的日志!!! log4j.appender.info.Threshold = INFO log4j.appender.info.layout = org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n log4j.appender.info.DatePattern = ‘.‘yyyy-MM-dd-HH-mm ### 只输出INFO级别的日志!!! log4j.appender.info.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter log4j.appender.info.filter.infoFilter.LevelMin=INFO log4j.appender.info.filter.infoFilter.LevelMax=INFO ### 保存Warn信息到单独文件 ### log4j.appender.warn = org.apache.log4j.DailyRollingFileAppender log4j.appender.warn.File = C:\\logs\\log4j\\warn.log log4j.appender.warn.Append = true ### 只输出WARN级别以上的日志!!! log4j.appender.warn.Threshold = WARN log4j.appender.warn.layout = org.apache.log4j.PatternLayout log4j.appender.warn.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n log4j.appender.warn.DatePattern = ‘.‘yyyy-MM-dd-HH-mm ### 只输出WARN级别的日志!!! log4j.appender.warn.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter log4j.appender.warn.filter.infoFilter.LevelMin=WARN log4j.appender.warn.filter.infoFilter.LevelMax=WARN ### 保存异常信息到单独文件 ### log4j.appender.error = org.apache.log4j.DailyRollingFileAppender log4j.appender.error.File = C:\\logs\\log4j\\error.log log4j.appender.error.Append = true ### 只输出ERROR级别以上的日志!!! log4j.appender.error.Threshold = ERROR log4j.appender.error.layout = org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n log4j.appender.error.DatePattern = ‘.‘yyyy-MM-dd-HH-mm ### 只输出ERROR级别的日志!!! log4j.appender.error.filter.infoFilter = org.apache.log4j.varia.LevelRangeFilter log4j.appender.error.filter.infoFilter.LevelMin=ERROR log4j.appender.error.filter.infoFilter.LevelMax=ERROR ### 子日志.在指定类中专用,必须指定包类名 ### ### 指定日志名调用:private static Logger log1=Logger.getLogger("com.zjw.main.TestLogSon"); ### 指定包类名调用:private static Logger log1=Logger.getLogger(TestLogSon.class); log4j.logger.com.zjw.main.TestLogSon=INFO,son,stdout log4j.appender.son = org.apache.log4j.DailyRollingFileAppender log4j.appender.son.File = C:\\logs\\log4j\\son\\TestLogSon.log log4j.appender.son.Append = true ### 只输出INFO级别以上的日志!!! log4j.appender.son.Threshold = INFO ### 不在root日志中输出,必须指定子日志全名 log4j.additivity.com.zjw.main.TestLogSon=false log4j.appender.son.layout = org.apache.log4j.PatternLayout log4j.appender.son.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n log4j.appender.son.DatePattern = ‘.‘yyyy-MM-dd-HH-mm ### 子日志.使用指定日志打印 ### ### 指定日志名调用:private static Logger log1=Logger.getLogger("log1"); log4j.logger.log1=INFO,son1,stdout log4j.appender.son1 = org.apache.log4j.DailyRollingFileAppender log4j.appender.son1.File = C:\\logs\\log4j\\son1\\son1.log log4j.appender.son1.Append = true ### 只输出INFO级别以上的日志!!! log4j.appender.son1.Threshold = INFO ### 不在root日志中输出,必须指定子日志全名 log4j.additivity.log1=false log4j.appender.son1.layout = org.apache.log4j.PatternLayout log4j.appender.son1.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n log4j.appender.son1.DatePattern = ‘.‘yyyy-MM-dd-HH-mm ### 子日志.在指定目录中使用 ### ### 在指定目录中正常调用:private static Logger log1=Logger.getLogger(Test.class); log4j.logger.com.zjw.main.test1=INFO,son2,stdout log4j.appender.son2 = org.apache.log4j.DailyRollingFileAppender log4j.appender.son2.File = C:\\logs\\log4j\\son2\\son2.log log4j.appender.son2.Append = true ### 只输出INFO级别以上的日志!!! log4j.appender.son2.Threshold = INFO ### 不在root日志中输出,必须指定子日志全名 log4j.additivity.com.zjw.main.test1=false log4j.appender.son2.layout = org.apache.log4j.PatternLayout log4j.appender.son2.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %c:%L %m%n log4j.appender.son2.DatePattern = ‘.‘yyyy-MM-dd-HH-mm ###参考log4j配置详解【https://www.cnblogs.com/ITtangtang/p/3926665.html

以上是关于log4j 详细配置实例的主要内容,如果未能解决你的问题,请参考以下文章

log4j配置实例

log4j.properties配置详解与实例

Log4j配置文件详解及实例

log4j.properties配置详解与实例

log4j.properties配置详解与实例-全部测试通过

elasticsearch多实例怎么配置