有时候在开发程序的工程中,无法适应application server的log4j加载,必须手工完成这个过程.其代码大约如下:
[code]
URL configFileUrl = LdapUtils.class.getResource("log4j.properties");
PropertyConfigurator.configure(configFileUrl);
[/code]
其中,
log4j.properties文件的放置路径为: bin/log4j.properties.如果该文件不是直接放在bin目录下,而是放在某个包中,这里假设是fred.study.configs,那么加载的代码就应该是:
[code]
URL configFileUrl = LdapUtils.class.getResource("/fred/study/configs/log4j.properties");
PropertyConfigurator.configure(configFileUrl);
[/code]
加载配置文件的类是LdapUtils
[code]
URL configFileUrl = LdapUtils.class.getResource("log4j.properties");
PropertyConfigurator.configure(configFileUrl);
[/code]
其中,
log4j.properties文件的放置路径为: bin/log4j.properties.如果该文件不是直接放在bin目录下,而是放在某个包中,这里假设是fred.study.configs,那么加载的代码就应该是:
[code]
URL configFileUrl = LdapUtils.class.getResource("/fred/study/configs/log4j.properties");
PropertyConfigurator.configure(configFileUrl);
[/code]
加载配置文件的类是LdapUtils
本文介绍了在开发过程中,如何通过编程方式手动配置Log4j的过程。提供了两种配置log4j.properties文件的方法:一种是在bin目录下直接配置,另一种是在特定包内配置。
886

被折叠的 条评论
为什么被折叠?



