zoukankan      html  css  js  c++  java
  • Log4j 配置文件(log4j.properties)的所在路径问题(转)

    转自:http://hi.baidu.com/oritenson/blog/item/968992523f6793998d543022.html
    一般我们直接将log4j.properties放置在src目录下,这样系统自动会找到的,其实就是放在WEB-INF/classes文件下。这个路径在classpath下,所以直接就能找到。我们写Logger的时候如下:

    public class HelloLog4j {
      
        public static Logger logger = Logger.getLogger(HelloLog4j.class);

        public static void main(String[] args) {       
            logger.debug("This is debug message.");
            logger.info("This is info message.");
            logger.error("This is error message.");
            xxx();
        }
        
        public static void xxx(){
            logger.debug("main method has invoked xxx method.");
        }
    }

    如果现在我们想把log4j.properties文件放置在其它目录下,例如:WEB-INF下和web.xml放在一起。这时候就需要我们手动指定log4j配置文件的路径,否则系统是找不到的。

    一、首先我们在web.xml中配置好log4j.properties路径:

            <context-param>
                <param-name>log4jConfigLocation</param-name>
                <param-value>/WEB-INF/log4j.properties</param-value>
            </context-param>

    二、然后写个servlet,部分代码如下:

    public void init() {
        String prefix = getServletContext().getRealPath("/");
        String file = getInitParameter("log4jConfigLocation");
        if (file != null) {
          PropertyConfigurator.configure(prefix + file);     
        }
    }

    三、在web.xml中配置servlet,并将log4jConfigLocation加入到Servlet中,让其Server启动即运行:

    <servlet>
       <servlet-name>your servlet</servlet-name>
       <servlet-class>your servelt class</servlet-class>
       <init-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>/WEB-INF/log4j.properties</param-value>
        </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>

  • 相关阅读:
    Linux下c开发 之 线程通信(转)
    mount -t nfs 的使用
    window共享linux下的文件 samba
    C/C++ 的使用
    php获取格式时间和时间戳
    php压缩文件夹
    php递归删除文件夹
    php生成文件夹(递归生成)
    QQ音乐API分析记录
    $(this)与this的区别
  • 原文地址:https://www.cnblogs.com/zhihaowang/p/10128435.html
Copyright © 2011-2022 走看看