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 theLevel.DEBUG
level of output - public void error (Object message)
using theLevel.ERROR
level of output - public void fatal (Object message)
using theLevel.FATAL
level of output - public void info (Object message)
using theLevel.INFO
level of output - public void warn (Object message)
using theLevel.WARN
level of output - public void trace (Object message)
using theLevel.TRACE
level 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