使用多个自定义滚动文件追加器时出现Log4j2问题

时间:2018-11-14 14:54:23

标签: apache log4j log4j2

我的log4j2属性文件如下所示

当我尝试使用以下代码访问附加器时

private static final Logger LOGGER = LogManager.getLogger("ejdk");
LOGGER.info("test");

它将日志记录在SeriesInResponse附加程序中,而不是在ejdk附加程序中。我的log4j2有多个自定义附加程序。我需要在不同的类文件中使用不同的附加程序,并且应该将条目记录在正确的文件中。

rootLogger.level = WARN
appenders = sdw,ejdk,SeriesInResponse
rootLogger.appenderRefs = sdw
rootLogger.appenderRef.sdw.ref = RollingFile
appender.sdw.type = RollingFile
appender.sdw.name = RollingFile
appender.sdw.fileName=G:/ApplicationLogs/sdw_internal_server.log
appender.sdw.filePattern=G:/ApplicationLogs/sdw_internal_server-%d{yyyy-MM- 
dd}.log.zip
appender.sdw.layout.type = PatternLayout
appender.sdw.layout.pattern = %d{ISO8601} %-5p [%t] (%13F:%L) %3x - %m%n
appender.sdw.policies.type = Policies

 # Rotate log file each day and keep 60 days worth
appender.sdw.policies.time.type = TimeBasedTriggeringPolicy
appender.sdw.policies.time.interval = 1
appender.sdw.policies.time.modulate = true
appender.sdw.strategy.type = DefaultRolloverStrategy
appender.sdw.strategy.delete.type = Delete
appender.sdw.strategy.delete.basePath = ${application.log}
appender.sdw.strategy.delete.maxDepth = 1
appender.sdw.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.sdw.strategy.delete.ifLastModified.age = 30d

loggers = ejdk,SeriesInResponse

logger.ejdk.name = ejdk
logger.ejdk.level = INFO
logger.ejdk.additivity = false
logger.ejdk.appenderRef.ejdk.ref = RollingFile
appender.ejdk.type = RollingFile
appender.ejdk.name = RollingFile
appender.ejdk.fileName=G:/ApplicationLogs/sdw_internal_ejdk.log
appender.ejdk.filePattern=G:/ApplicationLogs/sdw_internal_ejdk.log-%d{yyyy- 
MM-dd}.log.zip
appender.ejdk.layout.type = PatternLayout
appender.ejdk.layout.pattern = %d{ISO8601} %-5p [%t] (%13F:%L) %3x - %m%n
appender.ejdk.policies.type = Policies

# Rotate log file each day and keep 60 days worth
appender.ejdk.policies.time.type = TimeBasedTriggeringPolicy
appender.ejdk.policies.time.interval = 1
appender.ejdk.policies.time.modulate = true
appender.ejdk.strategy.type = DefaultRolloverStrategy
appender.ejdk.strategy.delete.type = Delete
appender.ejdk.strategy.delete.basePath = ${application.log}
appender.ejdk.strategy.delete.maxDepth = 1
appender.ejdk.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.ejdk.strategy.delete.ifLastModified.age = 30d

logger.SeriesInResponse.name = SeriesInResponse
logger.SeriesInResponse.level = INFO
logger.SeriesInResponse.additivity = false
logger.SeriesInResponse.appenderRef.SeriesInResponse.ref = RollingFile
appender.SeriesInResponse.type = RollingFile
appender.SeriesInResponse.name = RollingFile
appender.SeriesInResponse.fileName=G:/ApplicationLogs/sdw_internal_series_responses.log
appender.SeriesInResponse.filePattern=G:/ApplicationLogs/sdw_internal_series_responses.log-%d{yyyy-MM-dd}.log.zip
appender.SeriesInResponse.layout.type = PatternLayout
appender.SeriesInResponse.layout.pattern = %d{ISO8601} %m%n
appender.SeriesInResponse.policies.type = Policies

# Rotate log file each day and keep 60 days worth
appender.SeriesInResponse.policies.time.type = TimeBasedTriggeringPolicy
appender.SeriesInResponse.policies.time.interval = 1
appender.SeriesInResponse.policies.time.modulate = true
appender.SeriesInResponse.strategy.type = DefaultRolloverStrategy
appender.SeriesInResponse.strategy.delete.type = Delete
appender.SeriesInResponse.strategy.delete.basePath = ${application.log}
appender.SeriesInResponse.strategy.delete.maxDepth = 1
appender.SeriesInResponse.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.SeriesInResponse.strategy.delete.ifLastModified.age = 30d

1 个答案:

答案 0 :(得分:1)

您应该为附加程序使用不同的名称。

在您的配置中,所有内容都将添加到“ RollingFile”附加程序。

更改

appender.ejdk.name = RollingFile

appender.ejdk.name = RollingFileEjdk

logger.ejdk.appenderRef.ejdk.ref = RollingFile

logger.ejdk.appenderRef.ejdk.ref = RollingFileEjdk

类似地更正其他appanders和appenderRefs。