问题描述
我们正在使用log4j 2.5.我的问题是关于 ConferenceLog 的,因此请忽略下面的其他配置.
We are using log4j 2.5. My question is in regards to the ConferenceLog so please disregard other configurations below.
log4j2.xml文件:
log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="180">
<Properties>
<!-- change the path to conference logs below to fit your system -->
<Property name="LOG_DIR">C:\CodeRepos\logs</Property>
<Property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level
%logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}" />
</Console>
<Routing name="DailyLog">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="${LOG_DIR}/symproxy.log"
filePattern="${LOG_DIR}/symproxy-%i.log">
<PatternLayout pattern="${PATTERN}" />
<SizeBasedTriggeringPolicy size="20971520" />
<DefaultRolloverStrategy max="100" />
</RollingFile>
</Route>
</Routes>
</Routing>
<Routing name="ConferenceLog">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="${LOG_DIR}/conferences/${ctx:logFileName}.log"
filePattern="${LOG_DIR}/conferences/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="${PATTERN}" />
<SizeBasedTriggeringPolicy size="20971520" />
</RollingFile>
</Route>
</Routes>
</Routing>
<Routing name="DeviceLog">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="${LOG_DIR}/devices/${ctx:logFileName}.log"
filePattern="${LOG_DIR}/devices/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="${PATTERN}" />
<SizeBasedTriggeringPolicy size="5kb" />
<DefaultRolloverStrategy max="3" />
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="DailyLog" />
</Root>
<Logger name="conference.logger" level="debug" additivity="false">
<AppenderRef ref="ConferenceLog" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="device.logger" level="info" additivity="false">
</Logger>
</Loggers>
</Configuration>
我有一个小包装,在其中实例化了这个会议记录器
I have a small wrapper and in there i instantiate this conference logger
private final Log conferenceLogger = LogFactory.getLog("conference.logger");
然后像这样使用
private void logger(Long meetingId, Log log, String className, Level level, String message, Throwable error) {
ThreadContext.put(LOG_FILE_NAME, String.valueOf(meetingId));
logger(conferenceLogger, level, className + "." + message, error);
ThreadContext.remove(LOG_FILE_NAME);
}
private static void logger(Log log, Level level, String message, Throwable error) {
if (level == Level.DEBUG) {
log.debug(message);
} else if (level == Level.ERROR) {
if (error == null) {
log.error(message);
} else {
log.error(message, error);
}
} else if (level == Level.FATAL) {
log.fatal(message);
} else if (level == Level.TRACE) {
log.trace(message);
} else if (level == Level.INFO) {
log.info(message);
} else if (level == Level.WARN) {
log.warn(message);
}
}
这将创建一堆名为会议ID"的文件.但是,我的问题是这些文件在打开后再也不会关闭.
This creates a bunch of files named with the "meeting id". My problem however is that these files are never closed after they are opened.
我阅读了类似的SO帖子,但是我没有没有引用我的FileAppender
来调用close,也没有Logger
对象,因此我不确定如何实现其修复.如何以编程方式触发这些文件的关闭?因此,我们的一台服务器出现打开文件过多"错误.
I read over this similar SO post however I don't have references to my FileAppender
to call close on, nor do I have a Logger
object, so I'm not sure how to implement its fix. How do i trigger the closing of these files programmatically?One of our servers is getting the "too many open files" error due to this.
在阅读了文档之后,看起来IdlePurgePolicy应该是我所需要的,但是我看不到它对打开文件的数量有任何影响.如果我的log4j2文件有几个不同的地方,我尝试了下面的行,但是正如我提到的那样,它没有任何作用
After reading through the documentation more it seems like IdlePurgePolicy should be what I need but I do not see it having any effect on the number of open files.I tried the below line if a few difference places of my log4j2 file but as I mentioned, it had no effect
<IdlePurgePolicy timeToLive="1" timeUnit="minutes"/>
推荐答案
IdlePurgePolicy最终解决了此问题
IdlePurgePolicy did end up fixing this
<Routing name="ConferenceLog">
<Routes pattern="${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}"
fileName="${LOG_DIR}/conferences/${ctx:logFileName}.log"
filePattern="${LOG_DIR}/conferences/${ctx:logFileName}.%i.log.gz">
<PatternLayout pattern="${PATTERN}" />
<SizeBasedTriggeringPolicy size="20971520" />
</RollingFile>
</Route>
</Routes>
<IdlePurgePolicy timeToLive="30" timeUnit="seconds"/>
</Routing>
这篇关于由于log4j配置,打开的文件过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!