Spring 5.0 has been released for a while. Recently, the project was upgraded from Spring 4.3 to Spring 5.0. The class org.springframework.web.util.Log4jConfigListener used by Spring 4.3 to integrate log4j has been deleted in Spring 5.0, and log4j 1.x is no longer available. renew. We upgraded log4j-1.x to log4j-2.x
First introduce the three jar packages of log4j 2
log4j-api-2.10.0.jar
log4j-core-2.10.0.jar
log4j-web-2.10.0.jar
The web.xml file under the project is added. This setting allows the log file to be output to the web directory using ${web:rootDir}
<context-param> <param-name>log4jContextName</param-name> <param-value>myApplication</param-value> </context-param>
Log4j 2 supports four configuration methods: JSON, YAML, properties, and XML. We use properties to configure Log4j2 according to the old rules.
Add the log4j2.properties file to the src directory of the project, and spring 5.0 will automatically load this file by default
# Set the log level inside Log4j2, valid values: trace, debug, info, warn, error, fatal. It is only valid for the events of Log4j itself, you can not set it, when it is set to trace, you will see various detailed output inside log4j2 status = fatal # The name of the configuration name =PropertiesConfig appenders = console,I #Console type log output source appender.console.type = Console # output source name appender.console.name = consoleLog # output layout type appender.console.layout.type = PatternLayout #output template appender.console.layout.pattern = %m%n appender.console.target = System_out # The log output source of the file rolling record type appender.I.type = RollingFile # The name of the current scrolling output source so that it can be called in the Logger's configuration item appender.I.name = InfoRollingFile # The filename of the log file currently being operated on appender.I.fileName = ${web:rootDir}/WEB-INF/log/info.log # The filename format of the archived log file, where `%d{yyyy-MM-dd-HH}` is used to automatically fill in the date appender.I.filePattern = ${web:rootDir}/WEB-INF/log/info_%d{MM-dd}_%i.log # Scroll record output source layout type appender.I.layout.type = PatternLayout # Rolling record output template appender.I.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %c ] %m%n # Specify the archive strategy of the log file, which is mainly to complete the periodic log file archive work appender.I.policies.type = Policies # Divide logs based on time appender.I.policies.time.type = TimeBasedTriggeringPolicy # The interval of cutting is 1 month, that is, the log is archived once a day, if the file renaming rule configured in filePattern is ${web:rootDir}/WEB-INF/log/info_%d{yyyy-MM-dd HH- mm}-%i, the minimum time granularity is mm, that is, minutes, and the size specified by TimeBasedTriggeringPolicy is 1. Combined, a new file is generated every 2 minutes. If it is changed to %d{yyyy-MM-dd HH} and the minimum granularity is hour, a file will be generated every 2 hours. appender.I.policies.time.interval = 1 # Correction time range, counting from 0. If modulate=true, the archive time will be offset with 0 points as the boundary. For example, modulate=true, interval=4hours, then assuming that the last time the log was archived was 03:00, the next time the log was archived was 04:00, and the subsequent archive time appender.I.policies.time.modulate = true # Trigger strategy based on log file volume appender.I.policies.size.type = SizeBasedTriggeringPolicy # Trigger Rolling when the log file size is larger than the value specified by size appender.I.policies.size.size=50M # File archiving coverage strategy (RolloverStrategy) appender.I.strategy.type = DefaultRolloverStrategy # Generate the number of split (archived) files appender.I.strategy.max = 100 # Root log, parent node level order of all logs (low to high): all < trace <debug < info < warn < error < fatal <off rootLogger.level = debug rootLogger.appenderRef.I.ref = InfoRollingFile rootLogger.appenderRef.I.level = info # The output source associated with the name consoleLog pay attention to lowercase consolelog rootLogger.appenderRef.consolelog.ref = consoleLog # The production environment is set to off to turn off the console log output rootLogger.appenderRef.consolelog.level = debug
After the configuration is complete, write a class to test it
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class TestClass{ public static final Logger logger = LogManager.getLogger(TestClass.class); public void test(){ logger.info("Information...."); } }