The previous chapter describes the core components of log4j. This chapter describes how to use configuration files to configure these core components. Configuration log4j involves assigning levels, defining an append program, and specifying the layout object in the configuration file.
The Log4j.properties file is a key-value pair that holds the Log4j configuration properties file. By default, log management looks for a file named Log4j.properties in Classpath.
The root logger level is defined as debug and the connection add-in is named X to it
Setting an additional destination named X is a valid Appender
Set the layout's add-on X
Log4j.properties Syntax:
The following is a appender x syntax for the log4j.properties file:
= DEBUG, x# Set the appender named X to bes a File appenderlog4j.appender.x= for X Appenderlo G4j.appender.x.layout=Org.apache.log4j.patternlayoutlog4j.appender.x.layout.conversionpattern=%m %n
log4j.properties Example:
Using the syntax above, we define the Log4j.properties file as follows:
The level of the root logger (logger) is defined as debug and the connection add-in is named file
The add-in (appender) file is defined as Org.apache.log4j.FileAppender and written to a log directory named "Log.out"
The layout pattern defined is%m%n, which means that after each print log message, a line break is added
= DEBUG, file# Define the FILE appenderlog4j.appender.FILE= Org.apache.log4j.FileAppenderlog4j.appender.FILE.File=${log}/ for FILE Appenderlog4j.appender.FILE.layout= Org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n
It is important to note that Log4j supports UNIX-style variable substitution, such as ${variablename}.
Debug Level:
Use the debug two append program. All the possible options are:
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
All
These levels will be explained in the Log4j debug level article
Appenders:
Apache log4j provides Appender objects primarily responsible for printing log messages to different destinations, such as consoles, files, sockets,nt event logs, and so on.
Each Appender object has different properties associated with it, and these properties indicate the behavior of the object
Properties |
Description |
Layout |
Appender using the layout layouts object and the associated formatted Logging Information transformation mode |
Target |
The target can be a console, a file, or another project based on an add-on |
Level |
Level is required to control the filtering of log messages |
Threshold |
Appender can have a level threshold level associated with its independent logger level. Appender ignore any log messages with levels below the threshold level |
Filter |
The Filter object can parse out-of-level matching record information and decide whether the logged request should be handled by a specific Appender or ignored |
You can add a Appender object to the logger by setting the following in a configuration file that includes the following methods:
Log4j.logger. [Logger-name]=level, Appender1,appender. N
You can write the same structure in XML format as follows:
<name= "com.apress.logging.log4j" additivity= "false" > <ref= "Appender1"/> < ref= "Appender2"/></logger>
If you want to add a Appender object to a program, you can use the following method:
Public void Addappender (Appender Appender);
The Addappender () method adds a appender to the Logger object. As a sample configuration demonstration, you can add many Appender objects to the logger in a comma-delimited list, separating each print log information from the destination.
We use only one additional destination Fileappender in our example above. All possible additional destination options are:
We will cover Fileappender files and Jdbcappender records will be included in the database
Layout:
We use the patternlayout to use Appender. All the possible options are:
- Datelayout
- Htmllayout
- Patternlayout
- Simplelayout
- Xmllayout
Using Htmllayout and Xmllayout, you can format and generate logs in HTML and XML.
Layout format:
How to log information in Chapter format: Log format
LOG4J Tutorial 4, configuring