[英]External log4j.xml file
I am trying to run a jar with the log4j.xml file on the file system outside the jar like so: 我试图在jar外面的文件系统上运行带有log4j.xml文件的jar,如下所示:
java -jar MyJarName.jar -cp=/opt/companyName/pathToJar/ log4j.configuration=log4j.xml argToJar1 argToJar2
I have also tried: 我也尝试过:
java -jar MyJarName.jar -cp=/opt/companyName/pathToJar/ log4j.configuration=/opt/companyName/pathToJar/log4j.xml argToJar1 argToJar2
The log4j.xml is file is in the same directory as the jar (/opt/companyName/pathToJar/), yet I still get the standard warning message: log4j.xml文件与jar(/ opt / companyName / pathToJar /)位于同一目录中,但我仍然收到标准警告消息:
log4j:WARN No appenders could be found for logger (org.apache.axis.i18n.ProjectResourceBundle).
log4j:WARN Please initialize the log4j system properly.
Is it possible to have the config file outside the jar, or do I have to package it with the jar? 是否可以将配置文件放在jar外面,或者我是否必须将它与jar打包?
TIA TIA
When using the -jar
switch to launch an executable jar file, the classpath is obtained from the jar file's manifest. 使用
-jar
开关启动可执行jar文件时,将从jar文件的清单中获取类路径。 The -cp
switch, if given, is ignored. 如果给出
-cp
开关,则忽略该开关。
Jeff Storey's answer is going to be the easiest solution. Jeff Storey的答案将是最简单的解决方案。 Alternatively, you could add a
Class-Path
attribute to the jar file's manifest . 或者,您可以将一个
Class-Path
属性添加到jar文件的清单中 。
EDIT Try enabling log4j debugging, and making the path to log4j.xml
a fully-qualified URL. 编辑尝试启用log4j调试,并使
log4j.xml
的路径成为完全限定的URL。 For example: 例如:
java -Dlog4j.debug -Dlog4j.configuration=file:/path/to/log4j.xml -jar ...
这工作:
java -jar -Dlog4j.configuration="file:d:\log4j.xml" myjar.jar
For log4j2, use configurationFile option: 对于log4j2,请使用configurationFile选项:
java -Dlog4j.configurationFile=/path/to/log4j2.xml -jar ...
http://logging.apache.org/log4j/2.0/manual/configuration.html http://logging.apache.org/log4j/2.0/manual/configuration.html
你试过java -Dlog4j.configuration=/path/to/log4j.xml -jar <rest-of-args>
you can define a default properties file in the jar. 您可以在jar中定义默认属性文件。 if no custom properties is defined you can use this default file.if a custom property is defined you can override the default property.
如果未定义自定义属性,则可以使用此默认文件。如果定义了自定义属性,则可以覆盖默认属性。
myjar.jar file contains log4j.default.configuration myjar.jar文件包含log4j.default.configuration
you can run your program with this parameters to start your application 您可以使用此参数运行程序以启动应用程序
java -jar -Dlog4j.configuration=log4j.properties target\yourfile-v01_000_01_c002.jar
Example code 示例代码
public static void main(String[] args) {
String filename = System.getProperty("log4j.configuration");
if(null==filename||filename.trim().equals("")) {
logger.info("Using default log4j configuration log4j.default.properties.");
logger.info("If you would like to change the default configuration please add following param before startup -Dlog4j.configuration=<log4jfile>");
PropertyConfigurator.configure( Main.class.getResourceAsStream("/log4j.default.properties"));
} else {
File file = new File(filename);
if(!file.exists()) System.out.println("It is not possible to load the given log4j properties file :"+file.getAbsolutePath());
else PropertyConfigurator.configure(file.getAbsolutePath());
}
}
I had problems using log4j with Sun's JDK and automatic configuration. 我在使用Sun的JDK和自动配置的log4j时遇到了问题。
You can use this: 你可以用这个:
String filename = System.getProperty("log4j.configuration");
DOMConfigurator(filename);
before using Logger. 在使用Logger之前。
"-jar" only uses the classpath inside the executable jar, and -cp is ignored. “-jar”仅使用可执行jar内的类路径,并忽略-cp。 Adding "."
添加“。” to the classpath in the executable jar should allow log4j.xml to be fount.
到可执行jar中的类路径应该允许log4j.xml为fount。
java -cp "path/to/your/log4jxml:path/to/yourjar.jar" your.package.MainClass java -cp“path / to / your / log4jxml:path / to / yourjar.jar”your.package.MainClass
The log4j.xml in directory "path/to/your/log4jxml" will overwrite the log4j.xml file in "path/to/yourjar.jar". 目录“path / to / your / log4jxml”中的log4j.xml将覆盖“path / to / yourjar.jar”中的log4j.xml文件。
Please refer to Setting multiple jars in java classpath . 请参阅java classpath中的设置多个jar 。
The simplest path configuration for ``log4j2 is using the
static block setting
log4j.configurationFile`: ``log4j2最简单的路径配置
is using the
静态块setting
log4j.configurationFile`:
public class MyClass {
static {
System.setProperty("log4j.configurationFile", "./config/log4j2.xml");
}
protected final transient Logger logger = LogManager.getLogger(IDOLTracker.class);
public static void main(String[] args) {
logger.info("");
}
}
Then the structure could be as: 然后结构可以是:
ProgramFolder
ProgramFolder
|---- /config/log4j2.xml| ---- /config/log4j2.xml
|---- MyClass.jar| ---- MyClass.jar
When you run your jar, it will look outside the jar for the xml file. 当你运行你的jar时,它会在jar外面看到xml文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.