Log4j - How to route message to log file created dynamically

Java Conceptuel Diagram

About

How to route message to file created dynamically.

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

Example: Analytics Application

Definition

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.

Steps

Context Data

You need to put context data before logging.

Example:

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

Configuration: RoutingAppender

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

Below:

  • 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">
  <Appenders>
    <Routing name="Analytics" ignoreExceptions="false">
      <Routes pattern="${ctx:event_type}">
        <Route>
          <RollingFile
            name="analytics-${ctx:event_type}"
            fileName="logs/analytics/${ctx:event_type}.jsonl"
            filePattern="logs/analytics/$${date:yyyy-MM}/analytics-${ctx:event_type}-%d{yyyy-dd-MM-}-%i.jsonl.gz">
            <PatternLayout>
              <pattern>%m%n</pattern>
            </PatternLayout>
            <Policies>
              <TimeBasedTriggeringPolicy/>
              <SizeBasedTriggeringPolicy size="250 MB"/>
            </Policies>
          </RollingFile>
        </Route>
      </Routes>
      <!-- Created appender TTL -->
      <IdlePurgePolicy timeToLive="15" timeUnit="minutes"/>
    </Routing>
  </Appenders>
  <Loggers>
    <Logger name="net.bytle.api.http.AnalyticsLogger" level="debug" additivity="false">
      <AppenderRef ref="Analytics"/>
    </Logger>
  </Loggers>
</Configuration>

Documentation / Reference





Discover More
Java Conceptuel Diagram
Java - Log4j

Log4j is a debugging logger. version: 1.2 (2015 switch to 2.x) 2 Get a logger The name of this Logger will be the...



Share this page:
Follow us:
Task Runner