Languages
[Edit]
EN

Log4j 1.x - log levels (Java, Spring, Hibernate)

5 points
Created by:
isherwood
599

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 and OFF.

 

Warning: according to vulnerabilities it is recommended to use Log4j2.

 

Detailed description

Edit

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 from TRACE to FATAL levels will be logged,
  • by setting DEBUG level, messages from DEBUG to FATAL levels will be logged,
  • by setting INFO level, messages from INFO to FATAL levels will be logged,
  • etc.

In the below, you can find simple log levels summary that describes what they logs.

Log LevelDescription
ALLIt has the lowest possible rank and is intended to turn on all logging.
TRACEDesignates finer-grained informational events than the DEBUG.
DEBUGDesignates fine-grained informational events that are most useful to debug an application.
INFODesignates informational messages that highlight the progress of the application at coarse-grained level.
WARNDesignates potentially harmful situations.
ERRORDesignates error events that might still allow the application to continue running.
FATALDesignates very severe error events that will presumably lead the application to abort.
OFFIt has the highest possible rank and is intended to turn off logging.

    Source: Log4j 1.2 - API Docs

     

    Global configuration (log4j.properties file)

    Edit

    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:

     

    Practical example

    Edit

    In this section, you can find simple pure Java application that uses Log4j.

    Note: Log4j library can be found here.

    Output:

     

    Official documentation

    Edit

    We can learn more about loggers from Log4j documentation.

    The primary methods in the Logger class are listed below:

    Note: it is possible to define own levels by subclassing the Level class.

     

    References

    Edit
    1. Log4j 1.2 - Manual
    2. Log4j 1.2 Level Class - API Docs
    3. Logback's Architecture - Manual
    4. Log4j 2.0 Architecture - Manual
    1
    Donate to Dirask
    Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
    Join to our subscribers to be up to date with content, news and offers.
    Native Advertising
    🚀
    Get your tech brand or product in front of software developers.
    For more information Contact us
    Dirask - we help you to
    solve coding problems.
    Ask question.

    ❤️💻 🙂

    Join