Java 外部 log4j.xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2594689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:39:19  来源:igfitidea点击:

External log4j.xml file

javalogginglog4j

提问by javamonkey79

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

采纳答案by Jason Day

When using the -jarswitch to launch an executable jar file, the classpath is obtained from the jar file's manifest. The -cpswitch, if given, is ignored.

使用-jar开关启动可执行 jar 文件时,类路径是从 jar 文件的清单中获取的。该-cp开关,如果给定的,被忽略。

Jeff Storey's answeris going to be the easiest solution. Alternatively, you could add a Class-Pathattribute to the jar file's manifest.

Jeff Storey 的答案将是最简单的解决方案。或者,您可以Class-Path向 jar 文件的manifest添加一个属性。

EDITTry enabling log4j debugging, and making the path to log4j.xmla fully-qualified URL. For example:

编辑尝试启用 log4j 调试,并将路径设置log4j.xml为完全限定的 URL。例如:

java -Dlog4j.debug -Dlog4j.configuration=file:/path/to/log4j.xml -jar ...

回答by Jeff Storey

Have you tried java -Dlog4j.configuration=/path/to/log4j.xml -jar <rest-of-args>

你有没有尝试过 java -Dlog4j.configuration=/path/to/log4j.xml -jar <rest-of-args>

回答by Thorbj?rn Ravn Andersen

"-jar" only uses the classpath inside the executable jar, and -cp is ignored. Adding "." to the classpath in the executable jar should allow log4j.xml to be fount.

“-jar”仅使用可执行 jar 内的类路径,而忽略 -cp。加“.” 到可执行 jar 中的类路径应该允许输入 log4j.xml。

回答by tibi

this works:

这有效:

java -jar -Dlog4j.configuration="file:d:\log4j.xml" myjar.jar

回答by VlatkoB

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.

在使用记录器之前。

回答by Koray Güclü

you can define a default properties file in the 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.

您可以在 jar 中定义默认属性文件。如果未定义自定义属性,您可以使用此默认文件。如果定义了自定义属性,您可以覆盖默认属性。

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());

    }
}

回答by r00tGER

For log4j2, use configurationFileoption:

对于 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

回答by Dayong

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

回答by another

The simplest path configuration for ``log4j2is using thestatic blocksettinglog4j.configurationFile`:

``log4j2is using the静态块settinglog4j.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
|---- /config/log4j2.xml
|---- MyClass.jar

程序文件夹
|---- /config/log4j2.xml
|---- MyClass.jar

When you run your jar, it will look outside the jar for the xml file.

当您运行 jar 时,它将在 jar 外部查找 xml 文件。