Learn About The Process of Configuration of log4j

0
2262
configuration

In the last article, we discussed the architecture of log4j. In this article, we are going to discuss about the configuration of log4j in detail. This article will explain how we can configure the core components with the help of a log4j configuration file. The configuration of log4j in the configuration file includes the following steps.

• The log object Level assignment.
• Appender definition, and
• Layout objects specification in a configuration file.

The name of configuration file for log4j is log4j.properties. It maintains the properties as the key-value pairs. If the target PATH for log4j properties file is not set explicitly, then by default the LogManager will look for log4j.properties file in the CLASSPATH.

The log4j.properties file
The variable substitution in the log4j.properties file is of UNIX-style such as ${variableName}. In the earlier article, when we discussed the installation of log4j, we had learned to set the target path for the log4j.properties file in the pom.xml file.

In the below example, we have defined the root logger level as DEBUG with the Appender name ‘LOG_APP’ which is attached to it. In summary, we have to define below parameters which are associated with ‘LOG_APP’ (i.e. the Appender name).

• The Appender named LOG_APP should be a valid Appender.
• The stdout Appender.
• The Pattern to output the caller’s file name as well as line number.
• The file for LOG_APP Appender.
• Definition of the Max File Size for LOG_APP Appender.
• Definition of one backup file for LOG_APP Appender.
• Definition of the layout for LOG_APP Appender.

Below is the syntax for the log4j.properties file for an Appender LOG_APP.

The log object Level assignment
We can make good use of log object level by choosing the required log level out of the following 8 log levels present in the Log4j in order to filter the logs.

S No

Level

Description

1.

ALL

When we want to include all log levels.

2.

DEBUG

When we want to include fine-grained informational events which are the most useful logs to debug an application.

3

ERROR

When we want to include all the error events which may still allow the application to run continuously.

4.

FATAL

When we want to include very severe error events which will abort the application run prematurely.

5.

INFO

When we want to include the informational messages which highlight the progress of the application at a very coarse-grained level to be written on the logs.

6.

OFF

It is used when we want to turn off logging completely.

7.

TRACE

When we want to include more finer-grained informational events as compared to the DEBUG level.

8.

WARN

When we want to include potentially harmful situations to be written on the logs.

Log4j object level operates on the following rule, which is very simple to understand.

– A log request that is marked as level ‘m’ and a logger is levelled as ‘n’ will be enabled only if the priority of m >= n.

This is a simple rule of thumb which is obeyed by the Log4j system while setting up the log object level in any log4j application. In log4j, the object levels have the following priority order:

ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

The following is the syntax to set the custom log level in the log4j.properties files. Here, we are required to select one level out of ‘ALL / DEBUG / INFO / WARN / ERROR / FATAL / OFF’ levels in the log4j.properties file.

Log Appender definition
The Apache log4j has various Appender objects which publish the actual logging information to the several desired destinations such as a plain file on a hard disk, a database (i.e. Oracle, MySQL, etc.), and a log management system (LMS), a console, UNIX filesystem, etc. Each appender object has the distinct properties associated with itself that specifies the object behaviour in Log4j. Such properties are explained below.

S No.

Property

Description

1.

Filter

  • The Filter object is capable of analyzing the logging information beyond level matching.

  • It also helps to decide whether a particular Appender should control the logging requests or just ignore it.

2.

Layout

  • The Layout objects are used by the Appender which have the various conversion pattern associated with them in order to format the logging information.

3.

Level

  • The Level object is used to specify the required log level in order to control the log messages filtration.

4.

Target

  • The Target object is used to specify the target which may be a plain file on a hard disk, a database (i.e. Oracle, MySQL, etc.), and a log management system (LMS), a console, UNIX filesystem, etc. depending on the appender.

5.

Threshold

  • The threshold object is used to specify a threshold level associated with an Appender.

  • It is independent of the logger level.

  • As a rule of thumb, the Appender will simply ignore any logging messages that has a level lower than the specified threshold level.

We can add multiple appender object logger as a comma-separated list within the log4j.properties configuration file which has the following syntax.

Alternatively, we can also add the appender object to a logger through the application code which has the following syntax.

Below are the various appender options which are available with log4j module. We can select the appropriate appender to obtain the logs at the desired destination for our application.

S No.

Appender Options

1.

AppenderSkeleton

2.

AsyncAppender

3.

ConsoleAppender

4.

DailyRollingFileAppender

5.

ExternallyRolledFileAppender

6.

FileAppender

7.

JDBCAppender

8.

JMSAppender

9.

LF5Appender

10.

NTEventLogAppender

11.

NullAppender

12.

RollingFileAppender

13.

SMTPAppender

14.

SocketAppender

15.

SocketHubAppender

16.

SyslogAppender

17.

TelnetAppender

18.

WriterAppender

Layout objects
In the log4j.properties file, we had used the PatternLayout with our appender. We have the following options available for log4j for these layout objects.

S No.

Layout Object

Description

1.

DateLayout

DateLayout is used for date format layout.

2.

HTMLLayout

HTMLLayout is used to generate logs in HTML format.

3.

PatternLayout

It is a pattern layout. Example:

log4j.appender.APP.layout.ConversionPattern=%p %t %c – %m%n

4.

SimpleLayout

It is a simple layout to procure logs.

5.

XMLLayout

XMLLayout is used to procure logs in the XML format.

Conclusion: –
In this article, we discussed the configuration of log4j in the configuration file (i.e. log4j.properties) along with the suitable examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here