Log4j输出日志到项目路径下

在web.xml中设定webAppRootKey,以避免Spring加载log4j.properties时报“Webapprootsystempropertyalreadysettodifferentvalue”错误:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>webAppName.root</param-value>
 </context-param>
</web-app>

再在log4j.properties设置:

引用

log4j.rootLogger=INFO,FILE

log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender

log4j.appender.FILE.Threshold=DEBUG

log4j.appender.FILE.File=${webAppName.root}/WEB-INF/logs/webapp.log

log4j.appender.FILE.DatePattern='.'yyyy-MM-dd

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.ConversionPattern=%p\:%m%n%d%c(Line\:%L)[%t]%n