Log4j 1.x - log levels (Java, Spring, Hibernate)
In this article, we're going to have a look at log levels provided by the Log4j 1.x library.
The log levels are located in org.apache.log4j.Level
class.
Quick summary:
Log levels:
ALL
,TRACE
,DEBUG
,INFO
,WARN
,ERROR
,FATAL
andOFF
.
Warning: according to vulnerabilities it is recommended to use Log4j2.
The log levels were orderered from most detailed, across more and more cursory, and finally turned off that lets to log on different levels of details. It means each level contains logs from next levels (ALL
> TRACE
> DEBUG
> INFO
> WARN
> ERROR
> FATAL
> OFF
).
So:
- by setting
TRACE
level, messages fromTRACE
toFATAL
levels will be logged, - by setting
DEBUG
level, messages fromDEBUG
toFATAL
levels will be logged, - by setting
INFO
level, messages fromINFO
toFATAL
levels will be logged, - etc.
In the below, you can find simple log levels summary that describes what they logs.
Log Level | Description |
ALL | It has the lowest possible rank and is intended to turn on all logging. |
TRACE | Designates finer-grained informational events than the DEBUG . |
DEBUG | Designates fine-grained informational events that are most useful to debug an application. |
INFO | Designates informational messages that highlight the progress of the application at coarse-grained level. |
WARN | Designates potentially harmful situations. |
ERROR | Designates error events that might still allow the application to continue running. |
FATAL | Designates very severe error events that will presumably lead the application to abort. |
OFF | It has the highest possible rank and is intended to turn off logging. |
Source: Log4j 1.2 - API Docs
It is possible to configure the log level for the entire application by setting log4j.rootLogger
property in log4j.properties
file. Usually, the file is located under src/main/resources/log4j.properties
path, according to Maven projects Standard Directory Layout. By default, this configuration is used also by Spring Framwork and Hibernate ORM.
Example log4j.properties
file:
xxxxxxxxxx
# ...
log4j.rootLogger=TRACE
# Or one of:
#
# log4j.rootLogger=DEBUG
# log4j.rootLogger=INFO
# log4j.rootLogger=OFF
#
# etc.
# ...
In this section, you can find simple pure Java application that uses Log4j.
xxxxxxxxxx
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
public class Program {
private static final Logger log = Logger.getLogger(Program.class);
static {
BasicConfigurator.configure();
log.setLevel(Level.WARN);
}
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!");
}
}
Note: Log4j library can be found here.
Output:
xxxxxxxxxx
Warn message!
Error message!
Fatal message!
We can learn more about loggers from Log4j documentation.
The primary methods in the Logger
class are listed below:
xxxxxxxxxx
package org.apache.log4j;
public class Logger {
// Creation & retrieval methods:
public static Logger getRootLogger();
public static Logger getLogger(String name);
// Normal printing methods:
public void trace(Object message);
public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);
// Generic printing method:
public void log(Level level, Object message);
}
Note: it is possible to define own levels by subclassing the
Level
class.