Log4j - How to route message to log file created dynamically
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:
or if you use
slf4j as a facade
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
Documentation / Reference