One, add log4j dependency Package
The dependency package Log4j-x.x.xx.jar can be downloaded from the official website, and after the download build path, add dependency packages such as using 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>
Two, create the log4j.properties configuration file
The log4j.properties configuration file is used to configure the output format of the log, the address of the output ....
# # # 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
Third, configure the Load Log4j.properties property 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 time interval to refresh the log profile, which is set to 10s - <Context-param> <Param-name>Log4jrefreshinterval</Param-name> <Param-value>10000</Param-value> </Context-param> <!--load the log4j listener in the spring framework Log4jconfiglistener - <Listener> <Listener-class>Org.springframework.web.util.Log4jConfigListener</Listener-class> </Listener> <!--to avoid inter-project conflicts, define a unique Webapprootkey - <Context-param> <Param-name>Webapprootkey</Param-name> <Param-value>Scheduleproject</Param-value> </Context-param>
Four, use log4j in code to log records
Public class Test { privatestatic Logger Logger = Logger.getlogger (test.class); Public Static void Main (String[]args) { Logger.info ("23423"); Logger.warn ("SDFSDF");} }
Five, references
LOG4J Log Configuration Example
log4j log file configuration relative path in Spring MVC
Spring project configuration log4j logging feature