log4j2属性配置,用于将不同严重性的消息放入不同的日志文件中

时间:2018-02-10 19:23:24

标签: java log4j2

我尝试了所有可能性。我的 log4j2.properties 配置未正确生成。

我正在尝试为info, errordebug创建不同的文件。最后创建了文件。但是,没有记录日志。我花了一整天的时间。请帮忙。

name=PropertiesConfig
property.filename = c:\\logs
appenders = console, file, debug

appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

appender.file.type = RollingFile
appender.file.name = RollingFile
appender.file.level = info
appender.file.fileName = ${filename}/info.log
appender.file.filePattern = ${filename}.%d{yyyy-MM-dd}
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
appender.file.policies.type = Policies
appender.file.policies.time.type = TimeBasedTriggeringPolicy
appender.file.policies.time.interval = 1

appender.debug.type = RollingFile
appender.debug.name = RollingFile
appender.debug.level = debug
appender.debug.fileName = ${filename}/debug.log
appender.debug.filePattern = ${filename}.%d{yyyy-MM-dd}
appender.debug.layout.type = PatternLayout
appender.debug.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
appender.debug.policies.type = Policies
appender.debug.policies.time.type = TimeBasedTriggeringPolicy
appender.debug.policies.time.interval = 1

loggers = file,debug

logger.file.name = infoLogger
logger.file.level = info
logger.file.appenderRefs = RollingFile
logger.file.additivity = true
logger.file.appenderRef.rolling.ref = RollingFile

logger.debug.name = debugLogger
logger.debug.level = debug
logger.debug.appenderRefs = RollingFile
logger.debug.additivity = false
logger.debug.appenderRef.rolling.ref = RollingFile

rootLogger.level = INFO
rootLogger.appenderRefs = console, file, debug
rootLogger.appenderRef.stdout.ref = INFO

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您只想为一个日志级别创建一个日志文件。这可能有点难以实现(但可以按照下面的说明完成),因为日志级别被定义为最低级别。

例如;当您为调试设置日志级别时,此记录器可以记录调试及以上级别(信息,警告,错误,致命)。因此我们应该以某种方式禁用其他级别的日志记录要仅记录我使用LevelRangeFilter的调试级别。使用此属性,我们可以定义appender的级别范围。配置可以如下完成。 (我只添加信息和调试级别,但如果你愿意,可以添加其他级别)

https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/filter/LevelRangeFilter.html

name=PropertiesConfig
#output folder
property.filename = c:\\Testlogs
appenders = console, debugLoggerAppender, infoLoggerAppender

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

#debug appender
appender.debugLoggerAppender.type = File
appender.debugLoggerAppender.name = DEBUGLOGFILE
appender.debugLoggerAppender.fileName=${filename}/debug.log
appender.debugLoggerAppender.layout.type=PatternLayout
appender.debugLoggerAppender.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#using LevelRangeFilter to only log debug levels.
appender.debugLoggerAppender.filter.threshold.type = LevelRangeFilter
appender.debugLoggerAppender.filter.threshold.minLevel = debug
appender.debugLoggerAppender.filter.threshold.maxLevel = debug

#info appender
appender.infoLoggerAppender.type = File
appender.infoLoggerAppender.name = INFOLOGFILE
appender.infoLoggerAppender.fileName=${filename}/info.log
appender.infoLoggerAppender.layout.type=PatternLayout
appender.infoLoggerAppender.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
#using LevelRangeFilter to only log info levels.
appender.infoLoggerAppender.filter.threshold.type = LevelRangeFilter
appender.infoLoggerAppender.filter.threshold.minLevel = info
appender.infoLoggerAppender.filter.threshold.maxLevel = info

# creating only one logger, we can use this with multiple appenders.
loggers=fileLogger
# this is package name. This package and all of it's child packages will use this logger
logger.fileLogger.name=log.test
# logger base level
logger.fileLogger.level = debug
logger.fileLogger.appenderRefs = debugLoggerAppender,infoLoggerAppender
logger.fileLogger.appenderRef.debugLoggerAppender.ref = DEBUGLOGFILE
logger.fileLogger.appenderRef.infoLoggerAppender.ref = INFOLOGFILE

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

使用此配置

logger.debug("This is debug"); // this will be logged to console and debug file
logger.info("This is info"); // this will be logged to console and info file
logger.error("This is error"); // this will be only logged to console

同时检查此线程,它显示了如何使用xml配置完成此操作。 how to log only one level with log4j2?

答案 1 :(得分:0)

最后我明白了。下面的示例将为错误,调试创建单个文件,所有日志将打印在d:\ logs目录下的日志文件中

<强>的pom.xml

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.3.6</version>
</dependency>

,属性文件配置为......

<强> log4j2.properties

name=PropertiesConfig
#output folder
property.filename = d:\\logs
appenders = console, debugLoggerAppender, commonLoggerAppender, errorLoggerAppender

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

#common appender
appender.commonLoggerAppender.type = File
appender.commonLoggerAppender.name = RollingFile
appender.commonLoggerAppender.fileName=${filename}/log.log
appender.commonLoggerAppender.layout.type=PatternLayout
appender.commonLoggerAppender.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

#error appender
appender.errorLoggerAppender.type = RandomAccessFile
appender.errorLoggerAppender.name = RandomAccessFile
appender.errorLoggerAppender.fileName=${filename}/error.log
appender.errorLoggerAppender.layout.type=PatternLayout
appender.errorLoggerAppender.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#using LevelRangeFilter to only log error levels.
appender.errorLoggerAppender.filter.threshold.type = LevelRangeFilter
appender.errorLoggerAppender.filter.threshold.minLevel = error
appender.errorLoggerAppender.filter.threshold.maxLevel = error

#debug appender
appender.debugLoggerAppender.type = File
appender.debugLoggerAppender.name = LOGFILE
appender.debugLoggerAppender.fileName=${filename}/debug.log
appender.debugLoggerAppender.layout.type=PatternLayout
appender.debugLoggerAppender.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#using LevelRangeFilter to only log debug levels.
appender.debugLoggerAppender.filter.threshold.type = LevelRangeFilter
appender.debugLoggerAppender.filter.threshold.minLevel = debug
appender.debugLoggerAppender.filter.threshold.maxLevel = debug



# creating only one logger, we can use this with multiple appenders.
loggers=fileLogger
# this is package name. This package and all of it's child packages will use this logger
logger.fileLogger.name=com.fis.bsa
# logger base level
logger.fileLogger.level = debug
logger.fileLogger.appenderRefs = debugLoggerAppender,commonLoggerAppender, errorLoggerAppender
logger.fileLogger.appenderRef.debugLoggerAppender.ref = LOGFILE
logger.fileLogger.appenderRef.commonLoggerAppender.ref = RollingFile
logger.fileLogger.appenderRef.errorLoggerAppender.ref = RandomAccessFile

rootLogger.level = info
#rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

下面的属性应设置为使用AsyncLogger

System.setProperty("-DLog4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");