Originated from: http://hbiao68.iteye.com/blog/1947618
Take Dailyrollingfileappender as an example: Suppose a daily log file
The following settings are available:
Log4j.appender.a1=org.apache.log4j.dailyrollingfileappender
Log4j.appender.a1.file=app.log
Log4j.appender.a1.datepattern= '. ' Yyyy-mm-dd
Log4j.appender.a1.layout=org.apache.log4j.patternlayout
log4j.appender.a1.layout.conversionpattern=%d%5p-%c-%-4r [%t]-%m%n
Unable to find App.log file after testing yourself
To protect a log file: Under the root directory/web-info/logs/, the following 4 solutions are available for individuals:
1 Absolute path
log4j.appender.a1.file=d:\apache-tomcat-6.0.18/webapps/Project/web-inf/logs/app.log
But the wording is very flexible.
The same setting principle is used in the following 3: The JVM's environment variables
2:spring's Log4jconfiglistener
Use the following configuration XML code <context-param> <param-name>webAppRootKey</param-name> <param-value>web app.root</param-value> </context-param> <context-param> <param-name>log4jconfiglocation </param-name> <param-value>classpath:log4j.properties</param-value> </context-param> &L T;listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
Log4j.appender.logfile.file=${webapp.root}/web-inf/logs/app.log
Note:
1, spring configuration has changed Webapprootkey can not be changed, the value is free to write.
2, log files stored in the Tomcat project directory/webapp (project name)/web-inf/logs/app.log file.
2: Use existing JVM variables:
For example:
Log4j.appender.logfile.file=${user.home}/logs/app.log
The log will be located at: For example Windows:c:\documents and Settings\joe\logs\app.log
3 Set up your own directory, that is, when the project started through the System.setproperty settings, through the implementation of Servletcontextlistener to solve: such as Java code public class log4jlistener implements ServletContextListener { public static final string log4jdirkey = "Log4jdir"; public void contextdestroyed (servletcontextevent servletcontextevent) { system.getproperties (). Remove (Log4jdirkey); } public void contextinitialized ( servletcontextevent servletcontextevent) { string log4jdir = servletcontextevent.getservletcontext (). Getrealpath ("/"); // System.out.println ("Log4jdir:" +log4jdir); system.setproperty (Log4jdirkey, &NBSP;LOG4JDIR); &nbSp } }
Web.xml configuration: Java code <listener> <listener-class>com.log4j.log4jlistener</listener-class> </lis Tener>
Log4j.prtperties configuration:
Log4j.appender.a1.file=${log4jdir}/web-inf/logs/app1.log to solve.