Configure the log4j log function in the Spring Project, springlog4j
1. Add a log4j dependency package
You can download the dependency package log4j-x.x.xx.jar from the official website, after the download build path, add dependency packages such as maven, you can add the following Dependencies
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
2. Create the log4j. properties configuration file
The log4j. properties configuration file is used to configure the log output format and output address ....
### set log levels ###log4j.rootLogger = INFO, D, Elog4j.appender.D = org.apache.log4j.RollingFileAppenderlog4j.appender.D.File =${scheduleProject}WEB-INF/logs/schedule.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = DEBUGlog4j.appender.D.MaxFileSize = 50000KBlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%nlog4j.appender.E = org.apache.log4j.RollingFileAppenderlog4j.appender.E.File = ${scheduleProject}WEB-INF/logs/schedule.loglog4j.appender.E.Append = truelog4j.appender.E.Threshold = ERRORlog4j.appender.E.MaxFileSize = 50000KBlog4j.appender.E.layout = org.apache.log4j.PatternLayoutlog4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss} [ %l\:%c\:%t\:%r ] - [ %p ] %m%n
3. Configure the attributes for loading log4j. properties in the web. xml file.
<! -- Load the log4j configuration file log4j. properties --> <context-param> <param-name> log4jConfigLocation </param-name> <param-value>/WEB-INF/log4j. properties </param-value> </context-param> <! -- Set the interval for refreshing the log configuration file, set it to 10 s --> <context-param> <param-name> log4jRefreshInterval </param-name> <param-value> 10000 </param-value> </context- param> <! -- Load the log4j listener Log4jConfigListener in the Spring framework --> <listener-class> org. springframework. web. util. Log4jConfigListener </listener-class> </listener> <! -- To avoid project conflicts, define a unique webAppRootKey --> <context-param> <param-name> webAppRootKey </param-name> <param-value> scheduleProject </param-value> </context-param>
4. Use log4j for logging in the code
public class Test { private static Logger logger = Logger.getLogger(Test.class); public static void main(String[]args) { logger.info("23423"); logger.warn("sdfsdf"); }}
V. References
Log4j log configuration example
Relative Path of log4j log file configuration in Spring MVC