Mihnea R
Mihnea R

Reputation: 56

In Log4j2, using RollingFile appender, how to add an unique id to the rolled file name?

I'm using a RollingFile Appender with both Time (daily) and Size Trigger Policies.

The filePattern (used for renaming the file on rollover) contains:

filePattern="app-${sys:node}-%d{yyyyMMdd}-%i"

I would like to add a random ID (ideally a UUID) to the rolled file name.

Normally, the date pattern and the integer counter would be enough to unique identify a file...

but in my situation the logs are automatically picked up via FTP (and deleted if transferred successfully). Because of the file deletion it is possible to end up with a duplicate file (on the FTP, not locally)...

because the %i counter is based on the files that already exist locally.

For instance, having:

and log4j2 currently writing to, let's say : /tmp/app.log

If the 3 files already rolled are transfered via ftp and deleted , on the next rollover , I will have app-20180205-1.log instead of app-20180205-4.log. This is what I'm trying to avoid.

Any solutions?

Upvotes: 2

Views: 1018

Answers (1)

Dovmo
Dovmo

Reputation: 8749

Instead of a UUID, you can use a more specific date-time file pattern with a more specific time resolution instead, like so:

filePattern="logs/log4j2-demo-%d{yyyy-MM-dd-HH-mm-ss}-%i.log

When your Time/Trigger policies are met, it will create a new file with this more specific date-time stamp which should always be unique per the time resolution you specify.

Upvotes: 1

Related Questions