Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2: Dynamic creation of log files for multiple logs

I am currently creating a system that can have modules (think of them as plugins), where each one of them can have their own log, dedicated.

I would like to use the log4j2 project for logging, but I seem to have some trouble with the file appenders.

The main project (the module loader and "core" of the whole thing) should have its own log file, while the modules should have their own (like mod_XXXXXXXX.log).

By reading the documentation about the appenders I discovered the FileAppender class, and I was going to use that. Until I found out that I can't just simple add the appender to the default logger created by LogManager.getLog().

The logger returned by the LogManager is a different logger than the Logger interface.

Even searching did not give me any near solution, all I found was predefined file logs in the xml configuration - which is not what I want.

Thank you for reading; even the slightest clue is welcome :)

like image 907
spaceemotion Avatar asked Sep 27 '13 13:09

spaceemotion


1 Answers

if you really need to determine the log file dynamically, take a look at the Log4J2 RoutingAppender. A longer example is in the FAQ and these stackoverflow questions may be of interest: Wildcard pattern for RoutingAppender of Log4j2 and How to write different logs in different files with log4j2 (MDC in xml)?

Note that you need to set values in the ThreadContext map that the RoutingAppender uses to decide which appender to route the log event to. This means that you would need to put some value in the ThreadContext map every time your code enters a different plugin.

However, do you really need it to be this dynamic? If you know in advance what plugins you have, you can just declare a logger for each plugin (using the package name of the plugin is a common way to do this), and map each such logger to a separate appender.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <File name="MyFile" fileName="logs/app.log">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
    </File>
    <File name="plugin1" fileName="logs/plugin1.log">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
    </File>
    <File name="plugin2" fileName="logs/plugin2.log">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
    </File>
  </Appenders>
  <Loggers>
    <Logger name="com.mycomp.project.plugin1" level="debug">
      <AppenderRef ref="plugin1" level="debug" />
    </Logger>
    <Logger name="com.mycomp.project.plugin2" level="debug">
      <AppenderRef ref="plugin2" level="debug" />
    </Logger>
    <Root level="trace">
      <AppenderRef ref="MyFile" level="trace" />
    </Root>
  </Loggers>
</Configuration>
like image 92
Remko Popma Avatar answered Sep 30 '22 20:09

Remko Popma