使用不同的信息

时间:2015-05-27 19:15:12

标签: java log4j

我正在处理的API无法连接到数据库,但需要记录API中发生的事件。为此,我在考虑使用log4j创建包含API事件信息的日志文件。

问题是所有日志条目都在两个日志中结束,而不是分开。

要求我需要履行

  • 中包含特定信息的多个日志文件
  • 备份日志文件无限期生存

Log4j属性文件

log4j.rootLogger=QuietAppender, LoudAppender, FirstLog, SecondLog, TRACE

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n

# setup A1
log4j.appender.QuietAppender=org.apache.log4j.RollingFileAppender
log4j.appender.QuietAppender.Threshold=INFO
log4j.appender.QuietAppender.File=${wls.logs-path}/test-api/test-api-info.log
log4j.appender.QuietAppender.MaxFileSize=512KB
log4j.appender.QuietAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.QuietAppender.layout.ConversionPattern=%d %p [%c] - %m%n
# Keep three backup files.
log4j.appender.QuietAppender.MaxBackupIndex=100
# Pattern to output: date priority [category] - message
log4j.appender.QuietAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.QuietAppender.layout.ConversionPattern=%d %p [%c] - %m%n

# setup A2
log4j.appender.LoudAppender=org.apache.log4j.RollingFileAppender
log4j.appender.LoudAppender.Threshold=DEBUG
log4j.appender.LoudAppender.File=${wls.logs-path}/test-api/test-api-debug.log
log4j.appender.LoudAppender.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.LoudAppender.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.LoudAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.LoudAppender.layout.ConversionPattern=%d %p [%c] - %m%n

# setup FirstLog
log4j.appender.FirstLog=org.apache.log4j.RollingFileAppender
log4j.appender.FirstLog.Threshold=INFO
log4j.appender.FirstLog.File=${wls.logs-path}/test-api/first-info.log
log4j.appender.FirstLog.MaxFileSize=10240kB
log4j.appender.FirstLog.MaxBackupIndex=99999
log4j.appender.FirstLog.layout=org.apache.log4j.PatternLayout
log4j.appender.FirstLog.layout.ConversionPattern=%d %p [%c] - %m%n

# setup SecondLog
log4j.appender.SecondLog=org.apache.log4j.RollingFileAppender
log4j.appender.SecondLog.Threshold=INFO
log4j.appender.SecondLog.File=${wls.logs-path}/test-api/second-info.log
log4j.appender.SecondLog.MaxFileSize=10240kB
log4j.appender.SecondLog.MaxBackupIndex=99999
log4j.appender.SecondLog.layout=org.apache.log4j.PatternLayout
log4j.appender.SecondLog.layout.ConversionPattern=%d %p [%c] - %m%n

Java类

private static final Logger logkp = Logger.getLogger("FirstLog");
private static final Logger logda = Logger.getLogger("SecondLog");

logkp.info(sb.toString());
logda.info(sb.toString());

当前结果

2015-05-27 10:27:46,175 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6

2015-05-27 10:27:46,583 INFO [FirstLog] - APIServer1,Caller,test-Version-1.0,certValue,1
2015-05-27 10:28:22,458 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6

2015-05-27 10:28:22,793 INFO [FirstLog] - APIServer1,Caller,test-Version-1.0,certValue,1

2015-05-27 10:28:25,203 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6

2015-05-27 10:28:25,528 INFO [FirstLog] - APIServer1,Caller,test-Version-1.0,certValue,1

2015-05-27 10:28:26,686 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6

1 个答案:

答案 0 :(得分:1)

我不是100%肯定这个,因为我使用log4j已经有一段时间了,我们曾经用xml编写配置,但我认为你必须创建像这样的记录器:

log4j.rootLogger=QuietAppender, LoudAppender, TRACE
log4j.logger.FirstLogger = FirstLog, INFO
log4j.additivity.FirstLogger = false
log4j.logger.SecondLogger = SecondLog, INFO
log4j.additivity.SecondLogger = false

... // then configure appenders as you did

获得您想要的输出。将additivity设置为false会“切断”与rootLogger的连接。默认设置为true,将导致所有logmessages附加到记录器和记录器的所有祖先。将其设置为false将改变它。

如果您的API有自己的命名空间 - 让我们说“my.own.API”,那么您也可以创建一个这样的API-Logger:

log4j.logger.my.own.API = MyAPIAppender, INFO
log4j.additivity.my.own.API = false

并创建这样的记录器:

package my.own.API

public class MyAPIClass{
    private static Logger apiLog = Logger.getLogger(MyAPIClass.class);
    // ...
}