今天配置了log4j中写多个文件的内容,配置了半天才搞出来,为了避免类似问题,写个博客吧。
首先说一下需求,每天要在7个文件夹中生成文件,文件格式为xxx.log.2000.01.01,自己开发个写文件工具也不是不可以,但是一想log4j有现成的功能,何乐而不为呢,思想胜于一切。所以需要一个logger对应一个文件,开始配置,我定义了7个rootLogger,分别是
charge_log, gold_log, prop_log, register_log, active_log, game_play_log, online_log
</pre><pre name="code" class="plain">timer_log是错误日志记录。
log4j.rootLogger=INFO,stdout, timer_log, charge_log, gold_log, prop_log, register_log, active_log, game_play_log, online_log 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{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.appender.timer_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.timer_log.Append=true log4j.appender.timer_log.DatePattern='.'yyyy-MM-dd log4j.appender.timer_log.File=../logs/timer/timer.log log4j.appender.timer_log.Threshold=INFO log4j.appender.timer_log.layout=org.apache.log4j.PatternLayout log4j.appender.timer_log.layout.ConversionPattern=%d - %c [%t] %-5p %c %x %l - %m%n log4j.logger.charge_log=INFO, charge_log log4j.appender.charge_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.charge_log.Append=true log4j.appender.charge_log.DatePattern='.'yyyy-MM-dd log4j.appender.charge_log.File=../logs/charge_log/charge.log log4j.appender.charge_log.Threshold=INFO log4j.appender.charge_log.layout=org.apache.log4j.PatternLayout log4j.appender.charge_log.layout.ConversionPattern=%m%n log4j.additivity.charge_log=false log4j.logger.gold_log=INFO, gold_log log4j.appender.gold_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.gold_log.Append=true log4j.appender.gold_log.DatePattern='.'yyyy-MM-dd log4j.appender.gold_log.File=../logs/gold_log/gold_log.log log4j.appender.gold_log.Threshold=INFO log4j.appender.gold_log.layout=org.apache.log4j.PatternLayout log4j.appender.gold_log.layout.ConversionPattern=%m%n log4j.additivity.gold_log=false log4j.logger.prop_log=INFO, prop_log log4j.appender.prop_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.prop_log.Append=true log4j.appender.prop_log.DatePattern='.'yyyy-MM-dd log4j.appender.prop_log.File=../logs/prop_log/prop_log.log log4j.appender.prop_log.Threshold=INFO log4j.appender.prop_log.layout=org.apache.log4j.PatternLayout log4j.appender.prop_log.layout.ConversionPattern=%m%n log4j.additivity.prop_log=false log4j.logger.register_log=INFO, register_log log4j.appender.register_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.register_log.Append=true log4j.appender.register_log.DatePattern='.'yyyy-MM-dd log4j.appender.register_log.File=../logs/register_log/register_log.log log4j.appender.register_log.Threshold=INFO log4j.appender.register_log.layout=org.apache.log4j.PatternLayout log4j.appender.register_log.layout.ConversionPattern=%m%n log4j.additivity.register_log=false log4j.logger.active_log=INFO, active_log log4j.appender.active_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.active_log.Append=true log4j.appender.active_log.DatePattern='.'yyyy-MM-dd log4j.appender.active_log.File=../logs/active_log/active_log.log log4j.appender.active_log.Threshold=INFO log4j.appender.active_log.layout=org.apache.log4j.PatternLayout log4j.appender.active_log.layout.ConversionPattern=%m%n log4j.additivity.active_log=false log4j.logger.game_play_log=INFO, game_play_log log4j.appender.game_play_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.game_play_log.Append=true log4j.appender.game_play_log.DatePattern='.'yyyy-MM-dd log4j.appender.game_play_log.File=../logs/game_play_log/game_play_log.log log4j.appender.game_play_log.Threshold=INFO log4j.appender.game_play_log.layout=org.apache.log4j.PatternLayout log4j.appender.game_play_log.layout.ConversionPattern=%m%n log4j.additivity.game_play_log=false log4j.logger.online_log=INFO, online_log log4j.appender.online_log=org.apache.log4j.DailyRollingFileAppender log4j.appender.online_log.Append=true log4j.appender.online_log.DatePattern='.'yyyy-MM-dd log4j.appender.online_log.File=../logs/online_log/online_log.log log4j.appender.online_log.Threshold=INFO log4j.appender.online_log.layout=org.apache.log4j.PatternLayout log4j.appender.online_log.layout.ConversionPattern=%m%n log4j.additivity.online_log=false
配置文件中有几个关键点:
1.log4j.logger.online_log=INF
<pre name="code" class="java">import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestObservable { Logger chargeLogger = LoggerFactory.getLogger("charge_log"); Logger goldLogger = LoggerFactory.getLogger("gold_log"); Logger propLogger = LoggerFactory.getLogger("prop_log"); Logger registerLogger = LoggerFactory.getLogger("register_log"); Logger activeLogger = LoggerFactory.getLogger("active_log"); Logger gamePlayLogger = LoggerFactory.getLogger("game_play_log"); Logger onlineLogger = LoggerFactory.getLogger("online_log"); public void setSource(List<?> objectList,LoggerType loggerType) throws Exception { if(objectList == null || objectList.isEmpty()) { return; } for(Object obj : objectList) { switch(loggerType) { case CHARGE_LOG : chargeLogger.info(obj.toString()); break; case ACTIVE_ACCOUNT : activeLogger.info(obj.toString()); break; case GAME_PLAY : gamePlayLogger.info(obj.toString()); break; case GOLD_LOG : goldLogger.info(obj.toString()); break; case ONLINE_PLAYER : onlineLogger.info(obj.toString()); break; case PROP_LOG : propLogger.info(obj.toString()); break; case REGISTER_LOG : registerLogger.info(obj.toString()); break; default : throw new Exception("Type not suitable."); } } } public void setSource(Object obj,LoggerType loggerType) throws Exception { if(obj == null) { return; } switch(loggerType) { case CHARGE_LOG : chargeLogger.info(obj.toString()); break; case ACTIVE_ACCOUNT : activeLogger.info(obj.toString()); break; case GAME_PLAY : gamePlayLogger.info(obj.toString()); break; case GOLD_LOG : goldLogger.info(obj.toString()); break; case ONLINE_PLAYER : onlineLogger.info(obj.toString()); break; case PROP_LOG : propLogger.info(obj.toString()); break; case REGISTER_LOG : registerLogger.info(obj.toString()); break; default : throw new Exception("Type not suitable."); } } public static void main(String[] args) { TestObservable abservable = new TestObservable(); try { abservable.setSource("gole_log", LoggerType.GOLD_LOG); abservable.setSource("charge_log", LoggerType.CHARGE_LOG); abservable.setSource("active_log", LoggerType.ACTIVE_ACCOUNT); abservable.setSource("game_play_log", LoggerType.GAME_PLAY); abservable.setSource("online_player", LoggerType.ONLINE_PLAYER); abservable.setSource("prop_log", LoggerType.PROP_LOG); abservable.setSource("register_log", LoggerType.REGISTER_LOG);} catch (Exception e) { e.printStackTrace();}}} } }
O, online_log
2.log4j.additivity.online_log=false
additivity的含义是 子Logger 是否继承 父Logger 的 输出源(appender) 的标志位。具体说,默认情况下 子Logger 会继承 父Logger 的appender,也就是说 子Logger 会在 父Logger 的appender里输出。若是additivity设为false,则 子Logger 只会在自己的appender里输出,而不会在 父Logger 的appender里输出。
ConversionPattern=%m%n 是我只输出我的内容加个换行。