dsd
dsd

Reputation: 551

Log4j2: Log to a different file if already exists

If I run the same Java program multiple times in parallel, they obviously end up using the same log4j configuration. Now, I'd like each process to use an individual log file. I don't care much how they are differentiated (including the process id or a simple counter that increases with the number of already existing files would be enough). Is it possible to do this?

For instance, I'd like to configure the logger as follows:

<File name="File" fileName="/tmp/log-%i .log">
    <!-- ... -->
</File>

where %i is the next available integer.

Upvotes: 0

Views: 641

Answers (1)

blm
blm

Reputation: 2466

Here's something I think should do kind of what you want, although I haven't actually tried it. Use a RollingFile instead of a File, set the triggering policy to <OnStartupTriggeringPolicy/>, and set the filePattern to a pattern containing %i (or a date and time). The log file for the currently running process will be the fileName, but each time the process starts that file will be rolled over to one described by the filePattern.

Upvotes: 1

Related Questions