zoukankan      html  css  js  c++  java
  • log4j 路径环境变量配置和log4j加载配置

    1.lo4j日志路径从环境变量读取,log4j.xml配置如下:

    具体配置如下:
    log4j.appender.R.Encoding=UTF-8
    log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.R.Append=true
    #log4j.appender.R.Threshold=INFO
    log4j.appender.R.File=${log4j.home}/logs/log.log
    log4j.appender.R.DatePattern='.'yyyy-MM-dd
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

    1.1 监听配置web.xml

      <listener>
          <listener-class>com.jumper.logview.servlet.Log4jlistener</listener-class>  
      </listener>
      

    1.2 监听代码

    public class Log4jlistener implements ServletContextListener {
        final static String LOG4J_HOME = "log4j.home";
        final static String ENV_HOME = "LOG4J_HOME";
            
        public void contextDestroyed(ServletContextEvent servletcontextevent) {
            System.getProperties().remove(LOG4J_HOME);
        }
        
        public void contextInitialized(ServletContextEvent servletcontextevent) {
            String logsHome = System.getenv(ENV_HOME);
            if(logsHome == null){
                logsHome = System.getProperty("catalina.home")+"/log4j";
            }
    System.out.println("*********log4j dir:"+logsHome);        
            System.setProperty(LOG4J_HOME, logsHome);
        }
    }

    2.log4j 加载的两种方式

    2.1采用spring加载配置

         <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>/WEB-INF/classes/config/log4j.properties</param-value>
        </context-param>
        <!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 -->
        <context-param>
            <param-name>log4jRefreshInterval</param-name>
            <param-value>1000000</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>

    2.2采用serlvet加载

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import org.apache.log4j.PropertyConfigurator;
    public class Log4jInit extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        public void destroy() {
            super.destroy();
        }
    
        public Log4jInit() {
            super();
        }
    
        /**
         * Initialization of the servlet. <br>
         * 
         * @throws ServletException
         *             if an error occurs
         */
        public void init() throws ServletException {
            String file = this.getInitParameter("log4j");// 从web.xml配置读取,名字一定要和web.xml配置一致
            if (file != null) {
                PropertyConfigurator.configure(this.getServletContext().getRealPath(file));        
            }
        }
    }
    
    
    web.xml配置:
    <servlet>
      <servlet-name>Log4jInit</servlet-name>
      <servlet-class>com.jumper.log4j.Log4jInit</servlet-class>
      <init-param>
       <param-name>log4j</param-name>//这个是名字是下边路径配置的标识(好像KEY一样)
       <param-value>/WEB-INF/classes/config/log4j.properties</param-value>//这是容器初始化时候加载log4j配置文件的路径(这好像一个value);
      </init-param>
      <load-on-startup>1</load-on-startup> 
     </servlet>
  • 相关阅读:
    Win7,未识别的网络,设置为工作网络, 使客户端机器能访问该Win7.
    Canvas 笔记
    断电后, VS2010 ASPX设计页面提示缺少程序集,参数错误的解决办法.
    Viso问题及技巧。
    IE,VML 用Jquery 访问,总是报 Failed !
    C# url重写及二级域名
    Sitemap.xml在线生成站点与工具(转)
    C#.net 格式化日期(datetime.tostring())
    参数编码 完全解决方案 (转)
    关于SEO优化方案(网上整理的资料)
  • 原文地址:https://www.cnblogs.com/xunianchong/p/5654304.html
Copyright © 2011-2022 走看看