0
默认行为,我在Grails应用程序的工作,并希望记录在不同的文件的消息。我想在不同的文件中记录异常,正常和API日志。但根据Log4j的一般角色,如果我们将记录器级别设置为'Info',则警告并且错误消息也将开始记录在此文件中,同时我想将错误消息记录在不同的文件中。因此,我的错误信息将在错误文件和信息文件中被记录。虽然我想让'info'记录器只记录'info'级别的消息,而不是'error'级别的消息。而'错误'记录器只是记录错误消息。避免同一邮件的多个记录由于Log4j的
下面是我的Log4j的配置:
log4j = {
def layoutPattern = new PatternLayout("[%d{ISO8601}] %m \r\n")
def dailyRollingInfoFile = new DailyRollingFileAppender(
name:"rollingInfoFileAppender",
layout: layoutPattern,
//Path of the Log File
fileName:"C:\\MS-Logs\\Application\\MSLogs.log",
datePattern: "'.'dd-MM-yyyy")
def dailyRollingExceptionFile = new DailyRollingFileAppender(
name:"rollingExceptionFileAppender",
layout: layoutPattern,
//Path of the Log File
fileName:"C:\\MS-Logs\\Exceptions\\ExceptionLogs.log",
datePattern: "'.'dd-MM-yyyy")
def dailyRollingExceptionAPIFile = new DailyRollingFileAppender(
name:"rollingAPIFileAppender",
layout: layoutPattern,
//Path of the Log File
fileName:"C:\\MS-Logs\\API\\MS-NotificationsLogs.log",
datePattern: "'.'dd-MM-yyyy")
//For logging exceptions stack trace
appenders {
appender dailyRollingInfoFile
appender dailyRollingExceptionFile
appender dailyRollingExceptionAPIFile
}
root {
info 'rollingInfoFileAppender', additivity: false
debug 'rollingAPIFileAppender', additivity: false
error 'rollingExceptionFileAppender'
}
}
而现在,我这是怎么添加过滤器:
dailyRollingExceptionFile.addFilter(new org.apache.log4j.varia.LevelMatchFilter(levelToMatch:'ERROR', acceptOnMatch: true))
dailyRollingExceptionFile.addFilter(new org.apache.log4j.varia.DenyAllFilter())
//To make it sure that It will just Log, Messages by Info Logger
dailyRollingInfoFile.addFilter(new org.apache.log4j.varia.LevelMatchFilter(levelToMatch:'INFO', acceptOnMatch: true))
dailyRollingInfoFile.addFilter(new org.apache.log4j.varia.DenyAllFilter())
//To make it sure that It will just Log, Messages by API Logger
dailyRollingAPIFile.addFilter(new org.apache.log4j.varia.LevelMatchFilter(levelToMatch:'DEBUG', acceptOnMatch: true))
dailyRollingAPIFile.addFilter(new org.apache.log4j.varia.DenyAllFilter())
如何,它可能会避免相同的消息被在不同的文件twicely记录?我们如何在不同的文件中记录消息而不用在其他文件中重复它?
感谢您的时间:)
感谢@Andrew,其他的工作就像一个魅力 – 2014-09-19 18:05:32
能否请您让我明白,什么加属性的作用呢? – 2014-09-19 18:09:07
可加性与父/子记录器有关,而不是日志级别。 log4j的手册提供细节:http://logging.apache.org/log4j/2.x/manual/configuration.html#Additivity – 2014-09-19 18:41:57