Setting up Log4j2 in NetBeans, basic configuration

半世苍凉 提交于 2019-12-12 05:19:39

问题


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)


回答1:


  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.




回答2:


...
  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();
    //
    ...
  }
...


来源:https://stackoverflow.com/questions/43094299/setting-up-log4j2-in-netbeans-basic-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!