Log4j放在其他目录下

一般我们直接将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路径: log4jConfigLocation /WEB-INF/log4j.properties 二、然后写个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启动即运行:

your servlet your servelt class log4jConfigLocation /WEB-INF/log4j.properties 1 但是,如果放在SSH中时,servlet被封装了,还要这样写吗??当然不需要。但如果位置发生变化了,该如何处理呢? log4jConfigLocation \WEB-INF\classes\coo\log4j.properties org.springframework.web.util.Log4jConfigListener org.springframework.web.util.IntrospectorCleanupListener 注:在配置中“/”与”\”最终都被编译成“\”
放到src目录下 import java.io.IOException; import java.util.Properties; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class LogPathTest { public static void main(String[] args) throws IOException{ Properties pro = new Properties(); pro.load(Thread().currentThread()().getContextClassLoader().getResourceAsStream(“config\log\log4j.properties”)); PropertyConfigurator.configure(pro);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值