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:
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 |
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. |
Following are the steps to be performed to use log4j:
log4j.jar
in the lib
for TOMCAT_HOME/lib
and delete the juli*.jar
from lib
. logging.properties
from the TOMCAT_HOME/CONF
. 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: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).
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 |
18.119.11.28