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

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)

    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:

    # ...
    
    log4j.rootLogger=TRACE
    
    # Or one of:
    #
    # log4j.rootLogger=DEBUG
    # log4j.rootLogger=INFO
    # log4j.rootLogger=OFF
    #
    # etc.
    
    # ...

     

    Practical example

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

    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:

    Warn message!
    Error message!
    Fatal message!

     

    Official documentation

    We can learn more about loggers from Log4j documentation.

    The primary methods in the Logger class are listed below:

    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.

     

    References

    1. Log4j 1.2 - Manual
    2. Log4j 1.2 Level Class - API Docs
    3. Logback's Architecture - Manual
    4. Log4j 2.0 Architecture - Manual
    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