When using Spring framework, we can easily configure log4j for log management.
Spring 5.0 has been released for a while, and recently upgraded the project from Spring 4.3 to Spring 5.0. The class org.springframework.web.util.Log4jConfigListener used by Spring 4.3 integrates log4j has been deleted in Spring 5.0 version, and log4j version 1.x is no longer updated. We upgraded log4j-1.x to log4j-2.x
First introduce the three jar packages of log4j 2
Add the web.xml file under the project. 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 log4j2.properties file in the project's src directory. Spring 5.0 will automatically load the file by default
# Set the log level inside Log4j2, valid values: trace, debug, info, warning, error, fatal. Only valid for events in Log4j itself, it can be not set. When set to trace, you will see various detailed output status inside log4j2 status = fatal# Configuration name = PropertiesConfigappenders = 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%nappender.console.target = System_out # log output source appender.I.type = RollingFile# The name of the current scrolling output source can be called in the Logger configuration item appender.I.name = InfoRollingFile# The file name of the log file currently being operated appender.I.fileName = ${web:rootDir}/WEB-INF/log/info.log# The file name format of the archived log file, where `%d{yyyy-MM-dd-HH}` is used to automatically fill the date appender.I.filePattern = ${web:rootDir}/WEB-INF/log/info_%d{MM-dd}_%i.log# The scrolling record output source layout type appender.I.layout.type = PatternLayout# Scroll record output template appender.I.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %c ] %m%n# Specifies the storage policy for recording files. This strategy mainly completes periodic log file storage work appender.I.policies.type = Policies# Cut logs based on time appender.I.policies.time.type = TimeBasedTriggeringPolicy# The cutting interval is 1 month, that is, log archives are carried out 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, the size specified by TimeBasedTriggeringPolicy is 1, and combined to generate a new file every 2 minutes. If changed to %d{yyyy-MM-dd HH}, the minimum granularity is hours, a file is generated every 2 hours. appender.I.policies.time.interval = 1# Correct the time range, counting starts at 0. If modulate=true, the sealing time will be offset calculated with 0 points as the boundary. For example, modulate=true, interval=4hours, then assuming that the last time the log was blocked was 03:00, the next time the log was blocked was 04:00, and the subsequent time appender.I.policies.time.modulate = true# Triggering policy based on the volume of the log file appender.I.policies.size.type = SizeBasedTriggeringPolicy# When the log file volume is greater than the value specified by size, trigger Rollingappender.I.policies.size.size=50M# Overwrite policy for file storage (RolloverStrategy) appender.I.strategy.type = DefaultRolloverStrategy# Number of divided (sealed) files are generated appender.I.strategy.max = 100# Root log, the parent node level order of all logs (low to high): all < trace <debug < info < warn < error < fatal <offrootLogger.level = debugrootLogger.appenderRef.I.ref = InfoRollingFilerootLogger.appenderRef.I.level = info# Associate output source with name consoleLog Note consolelog lowercase rootLogger.appenderRef.consolelog.ref = consoleLog# Set the production environment to off to close the console log output rootLogger.appenderRef.consolelog.level = debugAfter the configuration is completed, write a class test
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..."); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.