This is the fourth day of my participation in Gwen Challenge
Log4j2
Log4j 2.x no longer supports file configuration with the. Properties suffix in 1.x, and the configuration file name extension in 2.x can only be “.xml”,”.json” or “.jsn”.
- Format of the configuration file: The log2j configuration file can be in XML format or JSON format.
- Location of configuration files: By default, log4j2 looks for files named log4j2. XML, log4j.json, log4j. JSN in the classpath directory.
The system selects the following configuration file priorities (from first to last) :
- File log4j2-test.json or log4j2-test. JSN in.classpath.
- File log4j2-test.xml under.classpath.
- File log4j2.json or log4j2. JSN in.classpath.
- Classpath log4j2.xml file.
The default name is log4j2.xml.
2. Configuration file node parsing
(1). The root node
- Configuration has two properties: Status and MonitorInterval, and two child nodes :Appenders and Loggers(indicating that multiple Appenders and Loggers can be defined).
- The status command is used to specify the level of log4j’s own printed logs.
- Monitorinterval Specifies the monitoring interval that log4J automatically reconfigures, in seconds, minimum 5s.
(2). Appenders node,
There are three common sub-nodes :Console, RollingFile, and File.
- The Console node defines the Appender for output to the Console.
- Name: Specifies the name of the Appender.
- Target :SYSTEM_OUT or SYSTEM_ERR. Generally, the default value is SYSTEM_OUT.
- PatternLayout: Output format, not set Default :%m%n.
- The File node is used to define the Appender for the output File to the specified location.
- Name: Specifies the name of the Appender.
- FileName: specifies the name of the destination log file with the full path.
- PatternLayout: Output format, not set Default :%m%n.
- The RollingFile node is used to define an Appender that automatically removes old files and creates new ones when the specified size is exceeded.
- Name: Specifies the name of the Appender.
- FileName: specifies the name of the destination log file with the full path.
- PatternLayout: Output format, not set Default :%m%n.
- FilePattern: specifies the name format of the new log file.
- Policies: Specifies the rolling log policy, that is, when to create a log file and export logs.
- TimeBasedTriggeringPolicy: Policies child nodes, rolling strategy based on time, the interval attribute is used to specify how long scroll, the default is 1 hour. Ulate = True modulate= True modulate= True modulate=true modulate=true modulate= True Rather than 7 am.
- SizeBasedTriggeringPolicy: Policies child nodes, rolling strategy based on the specified file size, the size attribute is used to define the size of each log file.
- DefaultRolloverStrategy: Used to specify the maximum number of log files in a folder to start deleting the oldest and creating new ones (via the Max attribute).
- The ThresholdFilter attribute: onMatch indicates whether the specified log level is DENY or ACCEPT. OnMismatch indicates whether the specified log level is DENY, ACCEPT or NEUTRAL
(3). Loggers node
There are two common types :Root and Logger.
- The Root node is used to specify the Root log for the project. If Logger is not specified separately, the Root log output is used by default
- Level: indicates the log output level. There are eight log output levels in ascending order: All < Trace < Debug < Info < Warn < Error < Fatal < OFF.
- Logger nodes are used to specify log forms separately, such as different log levels for classes under a given package.
- Level: indicates the log output level. There are eight log output levels in ascending order: All < Trace < Debug < Info < Warn < Error < Fatal < OFF.
- Name: specifies the class to which this Logger belongs or the package path to which the class belongs, inherited from the Root node.
- AppenderRef: Logger’s child node that specifies to which Appender the log should be exported. If this is not specified, it inherits from Root by default. If specified, it will output in both the specified Appender and Root’s Appender, and we can set Logger additivity=”false” to output only in our custom Appender.
(4). About log level.
-
Eight levels are listed in ascending order: All < Trace < Debug < Info < Warn < Error < Fatal < OFF.
-
All: indicates the lowest level. It is used to enable All logging.
-
Trace: it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace, it’s Trace.
-
Debug: Indicates fine-grained information events that are very helpful for debugging applications.
-
Info: Messages highlight the execution of the application at a coarse-grained level.
-
Warn: Logs of warning and lower levels are generated.
-
Error: Logs of Error information are output.
-
Fatal: logs every critical error event that will cause the application to exit.
-
OFF: indicates the highest level. It is used to disable all logging.
-
The program prints logs at or higher than the specified log level. The higher the log level is, the less logs are printed.
-
Parameter Description
-
%d{HH:mm:ss.SSS} indicates the output time to milliseconds
-
%t Displays the current thread name
-
%-5level Indicates the output log level. -5 indicates the left alignment and the output of five characters is fixed. If the characters are insufficient, 0 is added to the rightCopy the code
-
%logger outputs the logger name, no output because Root logger has no nameCopy the code
-
% MSG Log text
-
% n lineCopy the code
Other commonly used placeholders are:
%F Displays the file name of the class, for example, log4j2test. Java %L Displays the line number. %M Displays the method nameCopy the code
3. Required Maven dependencies
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
Copy the code
Four, the simplest configuration
<? xml version="1.0" encoding="UTF-8"? ><Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Copy the code
Five, more comprehensive configuration
<? xml version="1.0" encoding="UTF-8"? > <! -- Log levels and priorities: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --> <! --Configuration status, this is used to set the internal output of log4j2. You do not need to set this value. If you set it to trace, you will see the internal output of log4j2. --monitorInterval: Log4j automatically detects changes to configuration files and reconfigures itself, sets the interval --><configuration status="WARN" monitorInterval="30">
<! Define all appenders -->
<appenders>
<! -- Configuration of the output console -->
<console name="Console" target="SYSTEM_OUT">
<! -- Output log format -->
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</console>
<! -- The file will print out all the information. This log will be automatically cleared every time the program runs, as determined by the append property.
<File name="log" fileName="log/test.log" append="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<! -- This will print all the info level and below information, each time the size of the log exceeds the size, the size of the log will be automatically saved in the folder created by year and month and compressed, as an archive.
<RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/info.log"
filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
<! -- Console only outputs messages of level and above (onMatch).
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
<RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/warn.log"
filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<! -- DefaultRolloverStrategy default to a maximum of 7 files in the same folder
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/error.log"
filePattern="${sys:user.home}/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
</RollingFile>
</appenders>
<! An appender will not work until logger is defined and appender is appended.
<loggers>
<! -- Filter out spring and Mybatis DEBUG messages -->
<logger name="org.springframework" level="INFO"></logger>
<logger name="org.mybatis" level="INFO"></logger>
<root level="all">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileWarn"/>
<appender-ref ref="RollingFileError"/>
</root>
</loggers>
</configuration>
Copy the code