Log4j

Log4j is the project run by The Apache Software Foundation. This project helps in enabling the logs at the various levels of the server and application.

The major advantage of log4j is manageability. It provides the developer a freedom to change the log level at the configuration file level. Also, you can enable/disable logs at the configuration level, so there is no need to change the code. We can customize the log pattern based on the application, separately. Log4j has six log levels. The following figure shows the different types of log levels in log4j:

Log4j

Log level for log4j

Every log level in log4j has its own functionality. The following table shows the functionality of each log level in log4j:

Log level

Description

OFF

This level is set when you want logging to be set as false (Stopped logging).

FATAL

This log level will print the severe errors that cause premature termination.

ERROR

This log level is used to capture runtime errors or unexpected conditions.

Expect these to be immediately visible on a status console.

WARN

This level is used in the previous version.

It gives you almost errors, other runtime situations that are undesirable or unexpected, but not necessarily wrong.

Expect these to be immediately visible on a status console.

INFO

This log level will define the interesting runtime events (startup/shutdown).

It is best practice to put the logs at the INFO level.

DEBUG

Detailed information on the flow through the system is defined in this level.

TRACE

This log level captures all the events in the system and application.

How to use log4j

Following are the steps to be performed to use log4j:

  1. Download apache-log4j-1.2.X.tar.gz from its official URL http://logging.apache.org/log4j/1.2/download.html, where X is the minor version.
  2. Unzip the folder and place the log4j.jar in the lib for TOMCAT_HOME/lib and delete the juli*.jar from lib.
  3. Delete the logging.properties from the TOMCAT_HOME/CONF.
  4. Create a file log4j.properties in the TOMCAT_HOME/CONF and define the log appenders for the Tomcat instance. The following screenshot shows the appenders for catalina.out. Also, the highlighted code shows the roll-over of logs per day:
    How to use log4j

    Note

    You can customize the log rotation based on size, day, hour, and so on, using the previous log4j appenders marked in bold.

  5. Restart the Tomcat services.

    Note

    Important tip for the production environment

    DEBUG and TRACE modes are ideal modes for troubleshooting, but we have to disable them after log analysis. For a production environment, the ideal mode is INFO (DEBUG and TRACE generate heavy logging and also affect the server performance).

    Appenders should be enabled everyday in a production environment. This helps the administrator to perform a log analysis very easily (the file size is less).

Log level mapping

Until now, we have discussed the various log levels for JULI and log4j. Let us do a quick log level mapping for JULI and log4j. The following table shows the one-to-one mapping for log4j and JULI:

Log level in JULI

Log level in log4j

SEVERE

FATAL, ERROR

WARNING

WARN

INFO

INFO

CONFIG

NA

FINE

DEBUG

FINER

DEBUG

FINEST

TRACE

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.11.28