Here is the challenge I'm facing:
I have a servlet program. And I need to write logs for each user to the folder named after that user. Something like this:
// stores message to David folder
// /root_path/David/logfile.log
logger.error(MarkerManager.getMarker("David"), "Error happened");
// stores message to Mark folder
// /root_path/Mark/logfile.log
logger.error(MarkerManager.getMarker("Mark"), "Something is broken");
In my example I used markers. But I don't really know whether markers appropriate for this task.
In general my appender should behave like RollingRandomAccessFile appender. I guess config for appender must look like something like this:
<RollingRandomAccessFile name="rollingFile"
fileName="logs/{markerName ?????}/movie-db.log"
filePattern="logs/log-%d{yyyy-MM-dd}-%i.log.zip"
append="false"
immediateFlush="false"
ignoreExceptions="true">
<PatternLayout pattern="%d{ISO8601} %level{length=5} [%thread] %logger - %msg%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="25 MB"/>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingRandomAccessFile>
Any ideas?
Thanks to @Remko Popma answer I figure it out. Here is the solution example:
And log4j.xml:
The Log4j2 FAQ page has an example that uses the RoutingAppender to achieve this.