没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
一般我们直接将 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>
资源评论
- 蓝色多瑙河SF2018-09-19坑,真的太坑了,想不到别的话来评论
richardtxws
- 粉丝: 1
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功