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.
# Define the root logger with appender APP log4j.rootLogger=DEBUG, stdout, LOG_APP # add a ConsoleAppender to the logger stdout to write to the console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%x %5p [%t] - %d{yyyy-MM-dd HH:mm:ss.SSS}; - (%F:%L) - %m%n # Define the file for APP appender log4j.appender.LOG_APP=org.apache.log4j.RollingFileAppender log4j.appender.LOG_APP.File=Log4j_Example.log #Define Max File Size for APP Appender log4j.appender.LOG_APP.MaxFileSize=500KB # Keep one backup file for APP Appender log4j.appender.LOG_APP.MaxBackupIndex=1 # Define the layout for APP appender log4j.appender.LOG_APP.layout=org.apache.log4j.PatternLayout log4j.appender.LOG_APP.layout.ConversionPattern=%x %p %t - %d{yyyy-MM-dd HH:mm:ss.SSS}; - %c - %m%n
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.
# Define the root logger with appender APP log4j.rootLogger=ALL / DEBUG/ INFO /WARN / ERROR / FATAL / OFF, stdout, APP
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 |
|
2. |
Layout |
|
3. |
Level |
|
4. |
Target |
|
5. |
Threshold |
|
We can add multiple appender object logger as a comma-separated list within the log4j.properties configuration file which has the following syntax.
# Adding appender object to a logger log4j.logger. [logger-name] = level, appender1, appender2… appender n
Alternatively, we can also add the appender object to a logger through the application code which has the following syntax.
public void addAppender (Appender appender);
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.