Log4j2 overwrites past day log file
Asked Answered
L

4

3

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?

Leathery answered 22/3, 2018 at 15:9 Comment(3)
do you have more than one app writing to the same log file?Rubberneck
@FranMontero yes, I have other modules which uses JDK14Logger which saves to the same file. All was fine till switching Log4j to Log4j2Leathery
@FranMontero maybe simple I don't need RollingFile as current date is appended to the log filename anyway?Leathery
P
2

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.

Palma answered 24/3, 2018 at 19:28 Comment(2)
I need to use because of JDK6Leathery
@Leathery Updated answer.Palma
P
0

try

BasicConfigurator.resetConfiguration()

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

Paraclete answered 22/3, 2018 at 15:36 Comment(1)
I don't have BasicConfigurator.configure() in my codeLeathery
E
0

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"
Eschalot answered 4/10, 2019 at 15:28 Comment(0)
P
0

mention in time based TimeBasedTriggeringPolicy time interval as 1 it will rollover file on daylly basis

Patronage answered 7/10, 2019 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.