Log4j - How to route message to log file created dynamically

1 - About

How to route message to file created dynamically.

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

3 - Example: Analytics Application

3.1 - 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.

3.2 - Steps

3.2.1 - 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);

3.2.2 - 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>

4 - Documentation / Reference


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap