Log4j

Log4j Priority Example

This article is a tutorial about log priority levels in Log4j. In this tutorial, we are going to configure log4j via property files.

1. Introduction

Log4J (Java) is a widely used logging framework for Java. It continues to grow continuously with the recent upgrade to Log4j2. Log4j supports logging via Logger, Appender and Layouts.

Logger is the interaction point for the application and carries out the logging activity. It is used to specify the logging mode and the name of the logger. It also delivers logs to the specified destination with the help of the appender. The Appender delivers the log to the logging destination i.e. console, file or database along with options to fine-tune the logging mechanism. Appenders generally have lifecycle configuration and filtering support. Filtering enables to filter the messages whose logging mode does not match the level configured. Log4j supports multiple predefined appenders and also helps create custom appenders.

Layout specifies the display format of the logs. The most commonly used layout for Log4j is PatternLayout. A sample pattern is %d [%t] %-5p (%F: %L) – %m%n. The format strings for the pattern are as follows:

  • Date –  Full date till micro seconds.
  • Thread – JVM thread logging the output.
  • Logging Mode – INFO/ERROR/DEBUG/WARN.
  • Class – Java Class logging the output.
  • Line number – Line number in java class.
  • Message – The message logged.
  • Default line separator -/n unless specified otherwise.

2. Priority

Log4j supports the following priority levels:

  • OFF – turns off logging.
  • DEBUG – used generally for debugging purpose. i.e. development focused.
  • TRACE – similar to DEBUG but used to display more granular information for debugging.
  • INFO – used to display general information in logs. Mostly log analytic target these kind of logs. Hence it is the most used logging mode.
  • WARN – used to display warning but not errors. Most likely to indicate unmet dependencies and may or not result in errors.
  • ERROR – used to display errors in the application.
  • FATAL – used to display message just before the application halts.

Log4j needs to log in any of the modes specified above. They are listed out in increasing order of priority.

Example Class

package com.jcg.examples;
import org.apache.log4j.Logger;
public class LoggerMain {
public static final Logger logger = Logger.getLogger(LoggerMain.class);
public static void main(String[] args) {
while (true) {
logger.info("This is a warn log");
logger.debug("This is a debug log");
}
}
}
  • Line 4 configures a logger with name as the com.jcg.examples.LoggerMain.
  • Line 7,8 indicates the method used ie logging level for this message.

The next part is configuring the logger via XML file or properties. Below, we have used properties to provide a suitable configuration for the appender and the destination.

Configuration

log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
  • Line 1 specifies the threshold /allowed log level for the application.

Messages passing the threshold criteria will be displayed in the logging destination. The below screenshot shows the display of both the messages.

log4j unfiltered logs

3. Filtering based on priority

Log4j supports filtering based on mode specified. Considering the same java example but with the below configuration, the results would be different.

log4j.rootLogger=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n

DEBUG has less priority than WARN. Hence only messages logged with warn method will be displayed. Priority filters messages below the specified priority level. Generally in production, applications are run with error mode to filter other messages whereas in development environments it is set to DEBUG level to display all log messages to aid application debugging.

The below screenshot shows only the filtered logs.

log4j filtered logs

Here is another variation of the configuration producing the same result as above.

log4j.rootLogger=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n

We have specified the mode as INFO. INFO has higher priority than DEBUG but has less priority than WARN. So WARN messages will be displayed but DEBUG messages will be filtered.

4. Execution Steps

  1. Import the example as a Maven project
  2. Maven will import the dependencies automatically
  3. Stop the running project in eclipse after 5 minutes

5. Download the Source code

Download
You can download the full source code of this example here: Log4jPriorityExample

Rajagopal ParthaSarathi

Rajagopal works in software industry solving enterprise-scale problems for customers across geographies specializing in distributed platforms. He holds a masters in computer science with focus on cloud computing from Illinois Institute of Technology. His current interests include data science and distributed computing.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button