Log4j official document translation (five log output method)

Log class provides many methods for processing log activity, it does not allow ourselves to instantiate a logger, but two kinds of static methods available to us to get logger objects:

  • public static Logger getRootLogger ();
  • public static Logger getLogger(String name);

The first method returns the application instance root logger, it has no name.

The second method can be obtained by logging object logger name , the name of the class is the class name you pass, usually a class class name or package name.
static Logger log = Logger.getLogger(log4jExample.class.getName());

Log method

Once we have the example of the log, the message can be output by several of its methods. Logger class has several print log the following methods:

  • public void debug (Object message)
    using the Level.DEBUGlevel of output
  • public void error (Object message)
    using the Level.ERRORlevel of output
  • public void fatal (Object message)
    using the Level.FATALlevel of output
  • public void info (Object message)
    using the Level.INFOlevel of output
  • public void warn (Object message)
    using the Level.WARNlevel of output
  • public void trace (Object message)
    using the Level.TRACElevel of output

All levels are defined in org.apache.log4j.Level, the above-mentioned methods can be called something like the following:


import org.apache.log4j.Logger;
public class LogClass {
private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);
public static void main(String[] args) {
log.trace("Trace Message!");
log.debug("Debug Message!");
log.info("Info Message!");
log.warn("Warn Message!");
log.error("Error Message!");
log.fatal("Fatal Message!");
}
}

When the above code is executed, it will obtain:


Debug Message!
Info Message!
Warn Message!
Error Message!
Fatal Message!

In the next chapter will focus on explaining various levels.

Reproduced in: https: //my.oschina.net/u/204616/blog/545299

Guess you like

Origin blog.csdn.net/weixin_34208185/article/details/91989398