zoukankan      html  css  js  c++  java
  • log4j日志文件输出保存

    og4j.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/下:
    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 Settingsjoelogsapp.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
    来解决。
  • 相关阅读:
    linux下给U盘分区&制作文件系统
    迭代器 配接器
    仿函数
    在查询用户的权限的时候 使用左外连接 和 access数据库中左外连接
    C# 想要程序文件移动 而数据保持相对位置
    C# 第三方控件 下面的Item不显示了
    C# 第三方控件 错误 LC-1
    c# 第三方控件 闪退
    access 语句错误
    poj 1469(二分图 最大匹配)
  • 原文地址:https://www.cnblogs.com/staticking/p/7380252.html
Copyright © 2011-2022 走看看