Log4j Tutorial «Prev  Next»


Log4J Levels

Log4j, a highly versatile logging library in Java, provides various logging levels that allow developers to categorize and filter log messages based on their significance and urgency. When monitoring the activity of a Tomcat application, understanding and correctly utilizing these logging levels is crucial for effective log management and issue diagnosis. Below is a detailed explanation of the various levels that Loggers can be assigned when using Log4j:
  1. ALL:
    • Description: This is the lowest possible level and is intended to turn on all logging.
    • Use Case: When you aim to capture every log detail for comprehensive analysis or debugging, regardless of severity.
  2. TRACE:
    • Description: TRACE level is slightly more fine-grained than DEBUG and is typically used to capture detailed information about the flow and ehavior of the application.
    • Use Case: Utilized for deep troubleshooting, especially when you need insights into the application’s behavior at a granular level.
  3. DEBUG:
    • Description: DEBUG level is used to log detailed information that is diagnostically helpful to developers and support staff.
    • Use Case: Ideal for development and testing phases, where understanding the internal workings of the application is crucial.
  4. INFO:
    • Description: INFO level is used to log informational messages that highlight the progress of the application at a coarse-grained level.
    • Use Case: Suitable for production environments to capture routine operations, such as startup and shutdown sequences, or notable events during normal operation.
  5. WARN:
    • Description: WARN level is used to log potentially harmful situations that are not immediately problematic but might warrant investigation.
    • Use Case: Employed to highlight issues that are not errors but could potentially lead to problems, prompting preemptive action.
  6. ERROR:
    • Description: ERROR level is used to log error events that might still allow the application to continue running.
    • Use Case: Applicable when an issue has occurred that impacts some functionality but does not completely halt the application, such as a failure in a non-critical system component.

  7. FATAL:
    • Description: FATAL level is used to log very severe error events that might cause the application to abort.
    • Use Case: Utilized when a critical error has occurred, requiring immediate attention and possibly leading to application termination, such as a severe system failure or data corruption.
  8. OFF:
    • Description: This is the highest possible level, intended to turn off logging.
    • Use Case: Applied when you want to disable logging, either temporarily or for specific parts of the application.

By strategically assigning and utilizing these logging levels, a Java Developer can ensure that their Tomcat application’s logs are both informative and manageable. The granularity provided by levels like TRACE and DEBUG is invaluable during development and troubleshooting phases, while levels like INFO, WARN, and ERROR provide the necessary insights during production, ensuring proactive monitoring and prompt issue resolution. The ability to turn off logging with the OFF level also provides additional control, ensuring optimal performance and log management. Understanding and leveraging these levels effectively is key to maintaining a healthy, well-monitored application environment.

Loggers may be assigned levels. The set of possible levels, that is DEBUG, INFO, WARN, ERROR and FATAL are defined in the org.apache.log4j.Level class.
If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level. The root logger resides at the top of the logger hierarchy. It always exists and always has an assigned level. The logger is the core component of the logging process. In log4j, there are 5 normal levels Levels of logger available (not including custom Levels), the following is borrowed from the log4j API Apache Log4j 2
  1. static Level DEBUG: The DEBUG Level designates fine-grained informational events that are most useful to debug an application.
  2. static Level INFO: The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.
  3. static Level WARN: The WARN level designates potentially harmful situations.
  4. static Level ERROR:The ERROR level designates error events that might still allow the application to continue running.
  5. static Level FATAL: The FATAL level designates very severe error events that will presumably lead the application to abort.
In addition, there are two special levels of logging available: (descriptions borrowed from the log4j API Apache Log4j 2):
  1. static Level ALL: The ALL Level has the lowest possible rank and is intended to turn on all logging.
  2. static Level OFF: The OFF Level has the highest possible rank and is intended to turn off logging.

Ad Pro Apache Log4j