博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Log4j 配置文件(log4j.properties)的所在路径问题(转)
阅读量:6985 次
发布时间:2019-06-27

本文共 1428 字,大约阅读时间需要 4 分钟。

转自: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>

转载于:https://www.cnblogs.com/zhihaowang/archive/2011/08/24/10128435.html

你可能感兴趣的文章
deepin-安装问题:unable to find a medium containing a live file
查看>>
用 Hasor 谈一谈MVC设计模式
查看>>
IE 条件注释
查看>>
Windows热键注册(反汇编方法 查看win32api 原理)
查看>>
UNREFERENCED_PARAMETER的作用
查看>>
PHP计算表达式-栈
查看>>
IBATIS中关于iterate"$"与"#"的应用
查看>>
为什么要将对象序列化
查看>>
新增网址/网页 截图api[增加安全防护本接口已停用]源码可下载
查看>>
SpringMVC+Hibernate +MySql+ EasyUI实现POI导出Excel(二)
查看>>
刷leetcode第705题- 设计哈希集合
查看>>
dubbo协议参考
查看>>
SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
查看>>
读《白帽子讲Web安全》之安全意识篇(一)
查看>>
GLSL三种修饰符区别与用途(uniform,attribute和varying)
查看>>
python django django-debug-toolbar 加载缓慢,不能使用。
查看>>
操作系之进程调度及算法详解
查看>>
PHPexcel实列
查看>>
Butterknife 的简单使用 和 配合 Butterknife的插件 Zelezny
查看>>
Magento利用input type=”file”上传图片
查看>>