1

As a complete beginner, how do I set up Log4j2 (in Netbeans) to log some messages to the console and others into a file? (I only found guides for older versions, which got me into trouble because the XML changed. Basically, I’m updating what was done in this thread, because I thought it was a great idea but some tips were missing)

Community
  • 1
  • 1
Thomas
  • 395
  • 8
  • 21

2 Answers2

8
  1. You will need to download this archive of binaries

  2. Add “log4j-api-2.8.1.jar” and “log4j-core-2.8.1.jar” to your project. (in NetBeans: File -> Project Properties -> Libraries. I had to add it to both “compile” and “run”, or else I would get an error at runtime.)

  3. Tell Log4j where the file is. (in NetBeans: File -> Project Properties -> Run. Paste

    -Dlog4j.configurationFile=/path/to/your/file/log4j2.xml 
    

into the Textfield “VM-Options”

  1. Create an XML called log4j2.xml at the specified path (I did it in the main folder of my project) with your configuration, e.g.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <Configuration status="warn">
      <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
          <PatternLayout pattern="%m%n"/>
        </Console>
        <File name="FILE" fileName="logs/myLog.log">
          <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
          </PatternLayout>
        </File>
      </Appenders>
      <Loggers>
        <Root level="INFO">
          <AppenderRef ref="FILE" level="INFO"/>
          <AppenderRef ref="STDOUT" level="ERROR"/>
        </Root>
      </Loggers>
    </Configuration>
    

    This creates two appenders, a ConsoleAppender and a FileAppender, that log into System.out and a file called "myLog.log", respectively. Log messages of level INFO and higher are logged into the file, only errors are printed to the console.

  2. This is some sample code to demonstrate how Log4j2 is used:

    import java.io.IOException;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    public class Demo {
    
        private final static Logger log = LogManager.getLogger(Demo.class);
    
        public static void main(String[] args) throws IOException {
            log.info("starting...");
            try {
                ((Object) null).toString();
            } catch (Exception e) {
                log.error("error message");
            }
            log.info("some info.");
        }
    }
    

Running this leads to a console output of

    error message

while the file contains

    2017-03-29 14:37:20,675 INFO l.Demo [main] starting...
    2017-03-29 14:37:20,676 ERROR l.Demo [main] error message
    2017-03-29 14:37:20,676 INFO l.Demo [main] some info.

I hope this saved you some trouble, as it took me quite some time to figure this out. Feel free to edit this post - I have no idea how correct my statements are, these are just the steps that worked for me.

Thomas
  • 395
  • 8
  • 21
1
...
  private static Logger LOG;

  private static void loggerInit() {
    //
    //System.setProperty("log4j.configurationFile", ".../etc/log4j2.properties");
    System.setProperty("log4j.configurationFile", ".../etc/log4j2.xml");
    LOG = LogManager.getLogger(Tosser.class);
  }

  public static void main(String[] args) throws Exception {
    //
    loggerInit();
    //
    ...
  }
...