?
Log4j是我从接触Java以来一直使用的日志组件。由于Log4j的简单,易用,导致我工作至今始终没有在意过Log4j的配置问题。到现在还在使用一中配置来应对所有项目:
?
class="java" name="code"> 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,SSS} %5p %c{1}:%L - %m%n ### direct messages to file hibernate.log ### log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File=ipadserver.log log4j.appender.file.DatePattern = '.'yyyy-MM-dd log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n log4j.rootLogger=info, stdout, file
?随着项目的增长,日志文件变得非常庞大,使得定为问题编单异常困难。上面这个简单的配置带来的问题会让人很头疼:
?
被以上问题折腾了很久。今天对Log4j分包输出日志做了研究。
(如果你希望全面了解log4j,可以直接进这个传送门: http://blog.csdn.net/anlina_1984/article/details/5313023)
1. 在项目中创建4个包,分别是 log4.debug/error/info/warning
2. 在每个包下创建输出日志的测试代码:
log.debug("Debug in Debug Level"); log.info("Info in Debug Level"); log.warn("Warn in Debug Level"); log.error("Error in Debug Level");
?3. 在MainClass中调用日志输出:
package lv.showcase.log4j; import lombok.extern.log4j.Log4j; import lv.showcase.log4j.debug.DebugLog; import lv.showcase.log4j.error.ErrorLog; import lv.showcase.log4j.info.InfoLog; import lv.showcase.log4j.warning.WarnLog; @Log4j public class Main { public static void main(String[] args) { log.info("========== Log4j Showcase :"); log.debug("You can not see this message in info level."); DebugLog.log(); InfoLog.log(); WarnLog.log(); ErrorLog.log(); } }
?4. 采用如下log4j配置来控制日志输出:
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,SSS} %5p %c{1}:%L - %m%n log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=log4j_showcase.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n log4j.appender.R1=org.apache.log4j.FileAppender log4j.appender.R1.File=main.log log4j.appender.R1.layout=org.apache.log4j.PatternLayout log4j.appender.R1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n log4j.logger.lv.showcase.log4j.debug=debug, stdout, file log4j.logger.lv.showcase.log4j.error=error, stdout, file log4j.logger.lv.showcase.log4j.info=info, stdout, file log4j.logger.lv.showcase.log4j.warning=warn, stdout, file #Main函数的日志会输出到main.log文件中 log4j.logger.lv.showcase.log4j.Main=info, stdout, R1
?
(项目代码见附件)
?
控制台:
[java] 2013-08-06 10:22:01,684 INFO Main:? - ========== Log4j Showcase : [java] 2013-08-06 10:22:01,688 DEBUG DebugLog:? - Debug in Debug Level [java] 2013-08-06 10:22:01,689 INFO DebugLog:? - Info in Debug Level [java] 2013-08-06 10:22:01,689 WARN DebugLog:? - Warn in Debug Level [java] 2013-08-06 10:22:01,690 ERROR DebugLog:? - Error in Debug Level [java] 2013-08-06 10:22:01,691 INFO InfoLog:? - Info in Info Level [java] 2013-08-06 10:22:01,691 WARN InfoLog:? - Warn in Info Level [java] 2013-08-06 10:22:01,692 ERROR InfoLog:? - Error in Info Level [java] 2013-08-06 10:22:01,693 WARN WarnLog:? - Warn in Warning Level [java] 2013-08-06 10:22:01,693 ERROR WarnLog:? - Error in Warning Level [java] 2013-08-06 10:22:01,694 ERROR ErrorLog:? - Error in Error Level
?Main.log 输出:
2013-08-06 10:22:01,688 DEBUG DebugLog:? - Debug in Debug Level 2013-08-06 10:22:01,689 INFO DebugLog:? - Info in Debug Level 2013-08-06 10:22:01,689 WARN DebugLog:? - Warn in Debug Level 2013-08-06 10:22:01,690 ERROR DebugLog:? - Error in Debug Level 2013-08-06 10:22:01,691 INFO InfoLog:? - Info in Info Level 2013-08-06 10:22:01,691 WARN InfoLog:? - Warn in Info Level 2013-08-06 10:22:01,692 ERROR InfoLog:? - Error in Info Level 2013-08-06 10:22:01,693 WARN WarnLog:? - Warn in Warning Level 2013-08-06 10:22:01,693 ERROR WarnLog:? - Error in Warning Level
?
?
?