I have the following Log4J2 configuration XML:
<Routing name="myAppender">
<Routes pattern="$${ctx:workId}">
<Route>
<File fileName="${my-path}/sites/${ctx:workId}/${date:yyyy-MM-dd}/${ctx:employeeId}/emp.log" name="myAppender-${ctx:workId}">
<MarkerFilter marker="TELEMETRIC" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<Pattern>[%date{ISO8601}][%-5level][%logger{1.}][%marker][$${ctx:employeeId}] %X%m%n</Pattern>
</PatternLayout>
</File>
</Route>
</Routes>
</Routing>
<Async name="Async">
<AppenderRef ref="myAppender" level="info"/>
</Async>
However whenever the log4j2 appender writes to a file, it doesn't use the proper ${ctx:employeeId}
string when it writes to the specified file path.
The first time the log4j2 appender writes to a file, it writes to the correct file path specified by ${ctx:employeeId}
. But whenever information about an employee with another Id is put into the thread context, the appender is still logging to the old file path.
For example the first time the appender logs it writes to the correct path:
D:/example/logs/sites/1/2015-08-22/2/emp.log
But when the the logger is logging for the next employee Id (With a new ThreadContext) it is still logging into
D:/example/logs/sites/1/2015-08-22/2/emp.log
instead of for example:
D:/example/logs/sites/1/2015-08-22/3/emp.log
(Note the employee Id is different.)
In the log output pattern, I log the current employeeId [$${ctx:employeeId}]
, as well as what is in the current thread context %X
. The output shows that the correct employeeId is being used and is in the ThreadContext, but the appender is not logging to that ${ctx:employeeId}
file path.
Does anyone know if I am missing anything? Or if I am doing something wrong? Or is this possibly a bug with Log4j2? Thanks for any help!