Log4j - How to route message to log file created dynamically


How to route message to file created dynamically.

This is already embedded in the core log4j functionality when you combine:

Example: Analytics Application


This steps are for a analytics application that logs event (such as click button, video played,…)

Every event type must be routed to a different log files.


Context Data

You need to put context data before logging.


  • Log4j
String event_type = "video_play";
ThreadContext.put("event_type", event_type); 
  • or if you use slf4j as a facade
String event_type = "video play";
MDC.put("event_type", event_type);

Configuration: RoutingAppender

The RoutingAppender allows to route a message to an appender created dynamically.


  • There is one routing called Analytics
  • That defines Routes that defines a pattern that should match the key attribute of the Route.
  • As, there is only one Route, a log will go to the RollingFile route
  • The RollingFile use context data to create different appender based on its name
  • The IdlePurgePolicy defines when to delete this appender from memory.
  • The Logger node set this RoutingAppender to the Analytics class net.bytle.api.http.AnalyticsLogger
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Routing name="Analytics" ignoreExceptions="false">
      <Routes pattern="${ctx:event_type}">
              <SizeBasedTriggeringPolicy size="250 MB"/>
      <!-- Created appender TTL -->
      <IdlePurgePolicy timeToLive="15" timeUnit="minutes"/>
    <Logger name="net.bytle.api.http.AnalyticsLogger" level="debug" additivity="false">
      <AppenderRef ref="Analytics"/>

Documentation / Reference

Powered by ComboStrap