如何设置log4j属性文件? [英] How to set log4j property file?
问题描述
我有一个使用log4j的Eclipse Java项目.我无法设置要通过文件路径访问的log4j配置文件.我必须在jar中导出并运行该项目.
I have an Eclipse Java Project which uses log4j. I can't set the log4j configuration file to be accessed by file path. I have to export and run the project in a jar.
这是我的尝试方式:
public class Wita {
static Logger logger;
public static void main(String[] args) {
System.setProperty("log4j.configuration", new File("").getCanonicalPath()+File.separatorChar+"resources"+File.separatorChar+"log4j.xml" );
// System.out.println( System.getProperty("log4j.configuration") );
logger = Logger.getLogger(Wita.class.getName());
}
}
系统输出将打印出 C:\ Users \ roncsak \ eclipse_workspace \ WITA \ resources \ log4j.xml . WITA 是项目的基本文件夹.但是,使用 -Dlog4j.debug 参数运行项目时,也会返回以下内容:
System out prints the C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml which is good. WITA is the base folder of the project. But running the project with -Dlog4j.debug argument the following returns also:
log4j: Trying to find [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@18e3e60.
log4j: Trying to find [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml] using sun.misc.Launcher$AppClassLoader@18e3e60 class loader.
log4j: Trying to find [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml] using ClassLoader.getSystemResource().
log4j: Could not find resource: [C:\Users\roncsak\eclipse_workspace\WITA\resources\log4j.xml].
我想随着时间的推移更改log4j.xml,而不构建另一个jar文件.我该怎么办?
I would like to change the log4j.xml over time, without building another jar file. How can I do that?
推荐答案
来自
因此,您需要将 So you need to prepend 请参见 https://stackoverflow.com/a/7927278/603516 . 更好的代码: 这篇关于如何设置log4j属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
file:
放在log4j.configuration
属性值之前,以便可以将其视为URL.file:
to log4j.configuration
property value in order that it can be treated as URL. System.setProperty("log4j.configuration", new File("resources", "log4j.xml").toURL());