Log4j2覆盖过去一天的日志文件[英] Log4j2 overwrites past day log file

本文是小编为大家收集整理的关于Log4j2覆盖过去一天的日志文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用log4j2 ver 2.3

log4j2.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
    <RollingFile name="RollingFile"
                 fileName="${sys:catalina.base}/logs/catalina.${date:yyyy-MM-dd}.log"
                 filePattern="${sys:catalina.base}/logs/catalina.%d{yyyy-MM-dd}.log">
        <PatternLayout pattern="[TID=%X{TId}] %d{MMM dd, yyyy HH:mm:ss,SSS} %c %M:%L %p: %m%n"/>
        <TimeBasedTriggeringPolicy modulate="true" />
    </RollingFile>
</Appenders>
<Loggers>
    <Root level="DEBUG" >
        <AppenderRef ref="RollingFile" />
    </Root>
</Loggers>
</Configuration>

一切正常,直到第二天的日志被当天的一些日志覆盖.

示例: CATALINA.2018-03-21.LOG昨天(3月21日)很好,但今天在Catalina.2018-03-22.log中被一些日志所覆盖,从今天(3月22日)

有什么想法吗?

推荐答案

log4j 2.3有些古老,于2015-05-09发行,近3岁.因此,尝试使用更新版本;版本2.11.0是目前发行的最新版本,该版本于2018-03-11发行.

更新:继续使用log4j 2.3,您可以妥协您的要求.选项之一可能是fileName属性使用静态值.例如. .../catalina.log,.../catalina.current.log,等等

其他推荐答案

尝试

BasicConfigurator.resetConfiguration()

在使用Logger之后使用 basic Configurator.configure()

的方法中

其他推荐答案

尝试在文件模式(%i)中添加自动报重变量. 这对我来说很好.

filePattern="${sys:catalina.base}/logs/catalina.%d{yyyy-MM-dd}.%i.log"

本文地址:https://www.itbaoku.cn/post/978389.html

问题描述

I'm using Log4j2 ver 2.3

log4j2.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
    <RollingFile name="RollingFile"
                 fileName="${sys:catalina.base}/logs/catalina.${date:yyyy-MM-dd}.log"
                 filePattern="${sys:catalina.base}/logs/catalina.%d{yyyy-MM-dd}.log">
        <PatternLayout pattern="[TID=%X{TId}] %d{MMM dd, yyyy HH:mm:ss,SSS} %c %M:%L %p: %m%n"/>
        <TimeBasedTriggeringPolicy modulate="true" />
    </RollingFile>
</Appenders>
<Loggers>
    <Root level="DEBUG" >
        <AppenderRef ref="RollingFile" />
    </Root>
</Loggers>
</Configuration>

All works fine until next day when log from a previous day is getting overwritten by some logs from current day.

Example: catalina.2018-03-21.log yesterday (March 21st) was fine but today got overwritten by some logs from 2018-03-22 when catalina.2018-03-22.log contains rest of logs from today (March 22nd)

Any thoughts?

推荐答案

Log4j 2.3 is somewhat old, being released on 2015-05-09, almost 3 years old. So try using a updated version; version 2.11.0 is the latest as of now which was released on 2018-03-11.

Update: To continue using Log4j 2.3, you can compromise your requirements. One of the options could be using static value for fileName attribute. E.g. .../catalina.log, .../catalina.current.log, etc.

其他推荐答案

try

BasicConfigurator.resetConfiguration()

in the method that uses logger after BasicConfigurator.configure()

其他推荐答案

Try adding an auto-increment variable to your file pattern (%i). This worked fine for me.

filePattern="${sys:catalina.base}/logs/catalina.%d{yyyy-MM-dd}.%i.log"