以DailyRollingFileAppender 为ä¾ï¼å设æ¯å¤©ä¸ä¸ªæ¥å¿æ件
æ以ä¸è®¾ç½®ï¼
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
æ¤æ¶çææ¥å¿æ件å°ä½äºtomcatçbinç®å½ä¸ï¼å¦è¦å°æ¥å¿æ件ä¿åå¨ ï¼æ ¹ç®å½/web-info/logs/ä¸ï¼ä¸ªäººæ以ä¸4ç§è§£å³æ¹æ¡ï¼
1 ç»å¯¹è·¯å¾
log4j.appender.A1.File=D:\apache-tomcat-6.0.18/webapps/项ç®/WEB-INF/logs/app.log
ä½è¿ç§åæ³çµæ´»æ§å¾å·®
以ä¸3ä¸ä½¿ç¨ç¸åç设置åçï¼ jvmçç¯å¢åé
2ï¼springçLog4jConfigListener
éè¿ä»¥ä¸é
ç½®ï¼
< context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
< listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
...
log4j.appender.logfile.File=${webApp.root}/WEB-INF/logs/app.log
...
æ¥è§£å³
2:使ç¨å·²æjvmåéï¼
ä¾å¦ï¼
log4j.appender.logfile.File=${user.home}/logs/app.log
æ¥å¿å°ä½äºï¼ä¾å¦windowsï¼C:\Documents and Settings\joe\logs\app.log
3 èªå·±è®¾ç½®ç®å½ï¼ä¹å°±æ¯å¨é¡¹ç®å¯å¨æ¶éè¿System.setProperty设置
éè¿å®ç°ServletContextListeneræ¥è§£å³ï¼ä¾å¦
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, log4jdir);
}
}
web.xmlé
ç½®ï¼
<listener>
<listener-class>com.log4j.log4jlistener</listener-class>
</listener>
log4j.prtperties é
ç½®ï¼
log4j.appender.A1.File=${log4jdir}/WEB-INF/logs/app1.log
æ¥è§£å³ã
温馨提示:内容为网友见解,仅供参考