Log4j 配置文件


log4j.rootLogger=DEBUG,AA,BB


log4j.appender.AA=org.apache.log4j.ConsoleAppender
log4j.appender.AA.Target=System.err
log4j.appender.AA.layout=org.apache.log4j.SimpleLayout


log4j.appender.BB=org.apache.log4j.FileAppender
log4j.appender.BB.File=BookShop.log
log4j.appender.BB.layout=org.apache.log4j.PatternLayout
log4j.appender.BB.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n

其中:aa--->log日志打印到控制台上;

bb--->打印到tomcat/bin/BookShop.log文件夹里

如果想将日志保存到web-inf下 有以下四种方式

1. 绝对路径

log4j.appender.A1.File=D:\apache-tomcat-6.0.18/webapps/项目/WEB-INF/logs/app.log

这种写法灵活性很差

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

备注:

1》、spring配置变了webAppRootKey是不能变的,值可以随意写。

2》、log文件存放在tomcat工程目录/webapp(工程名称)/WEB-INF/logs/app.log文件。

3.使用已有jvm变量:

例如:

log4j.appender.logfile.File=${user.home}/logs/app.log

日志将位于:例如windows:C:\Documents and Settings\joe\logs\app.log

自己设置目录,也就是在项目启动时通过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 来解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值