
- log4j - Home
- log4j - Overview
- log4j - Installation
- log4j - Architecture
- log4j - Configuration
- log4j - Sample Program
- log4j - Logging Methods
- log4j - Logging Levels
- log4j - ConsoleAppender
- log4j - Logging in Database
Log4j - Formatting Layouts
- log4j - Log Formatting
- log4j - CSV Parameter Layout
- log4j - CSV Log Event Layout
- log4j - HTML Layout
- log4j - Pattern Layout
- log4j - Json Template Layout
Log4j - File Appenders
- log4j - Logging in Files
- log4j - FileAppender
- log4j - Separate Folder per Month
- log4j - Daily log File Generation
log4j Resources
log4j - Configuration
The previous chapter explained the core components of log4j. This chapter explains how you can configure the core components using a configuration file. Configuring log4j involves assigning the Level, defining Appender, and specifying Layout objects in a configuration file.
The log4j2.properties file is a log4j configuration file which keeps properties in key-value pairs. By default, the LogManager looks for a file named log4j2.properties in the CLASSPATH.
The level of the root logger is defined as DEBUG. The DEBUG attaches the appender named X to it.
Set the appender named X to be a valid appender.
Set the layout for the appender X.
log4j.properties Syntax:
Following is the syntax of log4j.properties file for an appender X:
# Define a console appender named X appender.0.type = Console appender.0.name = X appender.0.layout.type = PatternLayout appender.0.layout.pattern = %p - %m%n # Define the root logger with appender X rootLogger.level = INFO rootLogger.appenderRef.0.ref = X rootLogger.appenderRef.0.level = WARN
log4j.properties Example
Using the above syntax, we define the following in log4j.properties file:
The level of the root logger is defined as DEBUG, The DEBUG appender named MAIN to it.
The appender MAIN is defined with type File. It writes to a file named main.log located in the logs directory.
The layout pattern defined is %p - %m%n, which means the printed logging message will be followed by a newline character.
# Define the file appender appender.0.type = File appender.0.name = MAIN appender.0.fileName = logs/main.log appender.0.layout.type = PatternLayout appender.0.layout.pattern = %p - %m%n # Define the root logger with appender file rootLogger.level = INFO rootLogger.appenderRef.0.ref = MAIN rootLogger.appenderRef.0.level = DEBUG
It is important to note that log4j supports UNIX-style variable substitution such as ${variableName}.
Debug Level
We have used DEBUG with both the appenders. All the possible options are:
- TRACE
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
- ALL
These levels are explained later in this tutorial.
Appenders
Apache log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as consoles, files, sockets, NT event logs, etc.
Each Appender object has different properties associated with it, and these properties indicate the behavior of that object.
Property | Description |
---|---|
layout | Appender uses the Layout objects and the conversion pattern associated with them to format the logging information. |
target | The target may be a console, a file, or another item depending on the appender. |
level | The level is required to control the filtration of the log messages. |
threshold | Appender can have a threshold level associated with it independent of the logger level. The Appender ignores any logging messages that have a level lower than the threshold level. |
filter | The Filter objects can analyze logging information beyond level matching and decide whether logging requests should be handled by a particular Appender or ignored. |
We can add an Appender object to a Logger by including the following setting in the configuration file with the following method:
log4j.logger.[logger-name]=level, appender1,appender..n
You can write same configuration in XML format as follows:
<logger name="com.apress.logging.log4j" additivity="false"> <appender-ref ref="appender1"/> <appender-ref ref="appender2"/> </logger>
If you are willing to add Appender object inside your program then you can use following method:
public void addAppender(Appender appender);
The addAppender() method adds an Appender to the Logger object. As the example configuration demonstrates, it is possible to add many Appender objects to a logger in a comma-separated list, each printing logging information to separate destinations.
We have used only one appender FileAppender in our example above. All the possible appender options are:
AppenderSkeleton
AsyncAppender
ConsoleAppender
DailyRollingFileAppender
ExternallyRolledFileAppender
FileAppender
JDBCAppender
JMSAppender
LF5Appender
NTEventLogAppender
NullAppender
RollingFileAppender
SMTPAppender
SocketAppender
SocketHubAppender
SyslogAppender
TelnetAppender
WriterAppender
We would cover FileAppender in Logging in Files and JDBC Appender would be covered in Logging in Database.
Layout
We have used PatternLayout with our appender. All the possible options are:
JsonTemplateLayout
DateLayout
HTMLLayout
PatternLayout
SimpleLayout
XMLLayout
Using HTMLLayout and XMLLayout, you can generate log in HTML and in XML format as well.
Layout Formatting
You would learn how to format a log message in chapter: Log Formatting.