Log4j2 log configuration, log4j2 log
Background
Compared with log4j 1, log4j2. x has been reborn, and its official website claims that the advantages of multithreading are 10 times higher than log4j 1. high throughput of x and logback, configurable audit logs, and various flexible configurations based on plug-in architecture.
1. Required jar
1) maven Configuration
<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> </dependencies>
Add the following maven configuration to the webpage:
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> <scope>runtime</scope> </dependency>
2) use jar
log4j-api-2.7.jar log4j-core-2.7.jar
2. Configure the log Printing file
In the java project, the log4j2 configuration file will read the classpath to check whether the following files exist:
- Log4j. configurationFile
- Log4j2-test.properties
- Log4j2-test.yamlOrLog4j2-test.yml
- Log4j2-test.jsonOrLog4j2-test.jsn
- Log4j2-test.xml
- Log4j2. properties
- Log4j2. jsonOrLog4j2. jsn
- Log4j2. xml
- DefaultConfiguration
Add the log4j2. xml file in the main/src/resource Directory. The file content is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration status = "WARN"> <! -- Variable configuration --> <Properties> <Property name = "log_path" >$ {sys: catalina. home}/logs/zcrTest/</Property> </Properties> <! -- Appender configuration --> <Appenders> <Console name = "Console" target = "SYSTEM_OUT"> <PatternLayout pattern = "[% d] [% t] [% p] [% p] c: % L] % m % n "/> </Console> <RollingFile name =" DailyRollingFile "fileName =" $ {log_path}/zcrTest. log "filePattern =" $ {log_path}/zcrTest % d {yyyy-MM-dd }. log "> <PatternLayout pattern =" [% d] [% t] [% p] [% c: % L] % m % n "/> <TimeBasedTriggeringPolicy interval =" 1 "/> <SizeBasedTriggeringPolicy size =" 10 MB "/> </RollingFile> </Appenders> <Loggers> <Logger name = "mylog" level = "trace" additivity = "false"> <AppenderRef ref = "DailyRollingFile"/> </ logger> <! -- Configure the appender to be used -->
<! -- If additivity is enabled, the logger is printed twice because it meets the requirements of root. However, the level of the root logger is error. Why is the trace information in the Bar printed twice? -->
<Root level="info"> <AppenderRef ref="Console" /> <AppenderRef ref="DailyRollingFile"/> </Root> </Loggers></Configuration>
In the preceding log4j2. xml, Configuration is the root node and there is a status attribute, which indicates the log information printing level of log4j2. If you change the status to TRACE and run the test code, you can see that some log4j loading plug-ins, logger assembly, and other debugging information are printed on the console.
Log LevelTRACE <DEBUG <INFO <WARN <ERROR <FATAL for the low to high score. If it is set to WARN, information lower than WARN is not output. The level definition in Loggers also applies.
The preceding two log printing methods are configured. The printing level is info.
- Console: Print in command line
- RollingFile: print to file
Rolling means that after certain conditions are met, the original log file will be renamed for backup and a new log file will be generated. For example, if you want to generate a log file every day, but if the size of the log file within one day exceeds 1 GB, you can generate a new log file and meet one of the two conditions. This cannot be implemented in the native function of log4j 1. x, which is quite simple in log4j2.
RollingRandomAccessFile attributes:
- FileName specifies the location and name of the Current Log File
- FilePattern specifies the file transfer and rename rules when Rolling occurs.
- SizeBasedTriggeringPolicy specifies that when the file volume is greater than the size value, Rolling is triggered.
- Defaultroloverstrategy specifies the maximum number of files to be saved
TimeBasedTriggeringPolicy must be used with filePattern. Note that the file rename rule configured in filePattern is $ {log_path}/zcrTest % d {yyyy-MM-dd }. log. The minimum time granularity is dd, that is, minute. the size specified by TimeBasedTriggeringPolicy is 1. In combination, a new file is generated every day. If it is changed to % d {yyyy-MM-dd HH} and the minimum granularity is hour, a file is generated every hour.
3. Call
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;private static Logger logger = LogManager.getLogger(APIV1Filter.class);
logger.info(..);
logger.error(..)
4. Add the following configuration for a web project:
<Context-param> <param-name> log4jConfiguration </param-name> <param-value> "log4j2. xml directory "/log4j2. xml </param-value> </context-param> <listener-class> org. apache. logging. log4j. web. log4jServletContextListener </listener-class> </listener>
Thank you for reading this article!