Reputation: 1
I am attempting to add routing for log4j2 files to log to files with name containing their process id. This working with routing and specifying some code that injects a lookup for process id. However, the files no longer seem to archive correctly - every time I start the process, a new file is generated correctly with the process id, however nothing ever gets put in the Archive directory.
Also I've been unable to figure out how to keep at most say 5 files in archive even if I am not using Routing, but just RollingFile appender with an OnstartupTriggeringPolicy.
<Routing name="Routing">
<Routes pattern="$${process:Id}">
<Route>
<RollingFile name="logFile"
fileName="${LOG_DIR}/erdaemon-${process:Id}.log"
filePattern="${ARCHIVE}/erdaemon.%d{yyyy-MM-dd-hh-mm}.log">
<PatternLayout pattern="${PATTERN}" />
<Policies>
<OnStartupTriggeringPolicy />
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
<Async name="async" bufferSize="1000" includeLocation="true">
<AppenderRef ref="Routing" />
</Async>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="async"/>
</Root>
<Logger name="testlogger" level="TRACE" additivity="false">
<AppenderRef ref="async"/>
</Logger>
Upvotes: 0
Views: 565
Reputation: 11
I also encountered the same problem, the below config works for me:
change
filePattern="${ARCHIVE}/erdaemon.%d{yyyy-MM-dd-hh-mm}.log"
to
filePattern="${ARCHIVE}/erdaemon-${process:Id}.%d{yyyy-MM-dd-hh-mm}.log"
as you see, ${process:Id} must in your filePattern.
Upvotes: 0
Reputation: 9141
OnStartupTriggeringPolicy by itself can't trigger a "rollover" unless the new process happens to have the same process id of the previous process. Otherwise it will cause a new file to be created with a new name and not even know that a file from the previous execution existed.
I would suggest you create a Jira issue for Log4j 2 to create an action to do this.
As for keeping at most 5 files, the Delete action that is in version 2.5 could do this - but only if there is a rollover, which your configuration will never really perform.
Also, there is no particularly good reason I can see for you to use the Routing Appender in your configuration. Since a process can only have a single id you are never going to have more than a single RollingFileAppender.
Upvotes: 1