0

I'm trying to add a log4j2 rolling file appender at runtime. I have a process that has it's own log4j2.xml which logs to a file and the console. This process will receive request from other processes. It creates an output folder that I want to create a log file in for the output during that specific run. So, I add a rolling file appender in code, then remove it when the job is complete. This worked in log4j 1.x but doesn't work now that I updated to log4j2. Here's the code to add the appender:

     final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
     final Configuration config = loggerContext.getConfiguration();
     FileAppender appender =
           FileAppender.newBuilder().withName("MyFileAppender").withAppend(false)
           .withFileName(new File(myOutputDirectory, "log.txt").toString())
                 .withLayout(
                       PatternLayout.newBuilder().withPattern("%d [%t] %-5p %c - %m%n").build())
           .setConfiguration(loggerContext.getConfiguration()).build();
     appender.start();
     config.addAppender(appender);
     loggerContext.getRootLogger().addAppender(loggerContext.getConfiguration().getAppender(appender.getName()));
     loggerContext.updateLoggers();

The log.txt file is create but nothing is written to it. I also close the appender like this:

     if (appender != null && loggerContext != null)
     {
        appender.stop();
        Configuration config = loggerContext.getConfiguration();
        config.getRootLogger().removeAppender("OOEngineFileAppender");
        loggerContext.getRootLogger().removeAppender(appender);
        loggerContext.updateLoggers();
     }

Any idea what I'm doing wrong?

BTW, I turned on log4j2 debug via the command line switch and I see this output when I add the new appender:

DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger PluginManager 'Converter' found 44 plugins
DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false-2
DEBUG StatusLogger Starting FileManager e:\temp\OutputDir\TaskRun_20190402_215311_122\log.txt

Thanks, -Mike

Mike_G
  • 149
  • 2
  • 9
  • That the file is being created is a big clue. It means that your code is having an effect, which I think is at least half the battle. My guess is that the filtering on the appender is restricting any entries from being logged. I'm not specifically familiar with log4j2, as I use logback in my code. I would try setting the logging level specifically on the appender, if that makes sense for log4j2. – CryptoFool Apr 02 '19 at 18:04
  • In theory the "config.addAppender(appender) line should for the same level I specify in my log4j2.xml file. I also created a config and set that level to debug and added this new appender to that config and no luck. – Mike_G Apr 02 '19 at 21:56
  • This really baffles me. I added code to output the appenders found and I found the console, rollingfileappender and the new "MyFileAppender". So it's there but not writing. – Mike_G Apr 02 '19 at 22:49
  • Strange. The only thing I can think to do is follow a log operation through all the log4j code. I've done this before with logback to figure out what was going wrong when our log statements weren't making it to ElasticSearch. I was able to follow it pretty easily. – CryptoFool Apr 02 '19 at 22:55

0 Answers0