Search code examples
log4j2consoleappender

What is the purpose of log4j ConsoleAppender follow parameter?


In the example I am following, there is a line that goes like this in the log4j configuration file:

<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">

I understood everything in this line except for follow="true", I have read the description of this particular parameter on the official website: This Website. I also tried searching in other websites, but to no avail.

The description goes like this: follow parameter

This description got me confused, What do they mean by "honors reassignments....." , what is its purpose, what happens if I change it to false.


Solution

  • The System.out property is not read-only and can be reassigned through System#setOut. With follow="true" the appender will send the message to the current value of System.out, whereas with follow="false" logs will be sent to the original value of System.out.

    You can test the two behaviors with:

          // This performs automatic initialization
          final Logger logger = LogManager.getLogger(Log4j2Test.class);
          // System.out is attached to the JVM's `stdout`
          logger.warn("Sent to 'stdout'");
          // Reassignement of System.out
          System.setOut(new PrintStream(new File("file.log")));
          // System.out is attach to the file 'file.log'
          logger.warn("Sent to 'file.log'");
    

    From a practical perspective performance with follow="true" is worse than with follow="false" and System.out is rarely reassigned.