With log4j we are provided with methods to print logs with different levels.
1) public void debug(Object message)
It will print messages with the level Level.DEBUG
2) public void error(Object message)
It will print messages with the level Level.ERROR
3) public void fatal(Object message)
It will print messages with the level Level.FATAL
4) public void info(Object message)
It will print messages with the level Level.INFO
5) public void warn(Object message)
It will print messages with the level Level.WARN
6) public void trace(Object message)
It will print messages with the level Level.TRACE
Example:
import org.apache.log4j.Logger; public class LoggingExample { private static Logger log = Logger.getLogger(LoggingExample.class); public static void main(String[] args) { log.trace("Trace Log Message"); log.debug("Debug Log Message"); log.info("Info Log Message"); log.warn("Warn Log Message"); log.error("Error Log Message"); log.fatal("Fatal Log Message"); } }
Now suppose log level set in configuration file is WARN, then below will be output of above program:
Output
2014-12-05 09:14:42 WARN LoggingExample:12 - Warn Log Message 2014-12-05 09:14:42 ERROR LoggingExample:13 - Error Log Message 2014-12-05 09:14:42 FATAL LoggingExample:14 - Fatal Log Message