外部 log4j.xml 文件

发布于 2024-08-28 04:54:47 字数 677 浏览 4 评论 0原文

我正在尝试在 jar 外部的文件系统上运行带有 log4j.xml 文件的 jar,如下所示:

java -jar MyJarName.jar -cp=/opt/companyName/pathToJar/ log4j.configuration=log4j.xml argToJar1 argToJar2

我也尝试过:

java -jar MyJarName.jar -cp=/opt/companyName/pathToJar/ log4j.configuration=/opt/companyName/pathToJar/log4j.xml argToJar1 argToJar2

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.

是否可以将配置文件放在 jar 之外,或者我必须将其与 jar 一起打包吗?

TIA

I am trying to run a jar with the log4j.xml file on the file system outside the jar like so:

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: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?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

浊酒尽余欢 2024-09-04 04:54:47

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

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

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

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

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

Jeff Storey's answer is going to be the easiest solution. Alternatively, you could add a Class-Path attribute to the jar file's manifest.

EDIT
Try enabling log4j debugging, and making the path to log4j.xml a fully-qualified URL. For example:

java -Dlog4j.debug -Dlog4j.configuration=file:/path/to/log4j.xml -jar ...
最丧也最甜 2024-09-04 04:54:47

这有效:

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

this works:

java -jar -Dlog4j.configuration="file:d:\log4j.xml" myjar.jar
你在看孤独的风景 2024-09-04 04:54:47

对于 log4j2,使用 configurationFile 选项:

java -Dlog4j.configurationFile=/path/to/log4j2.xml -jar ...

http://logging .apache.org/log4j/2.0/manual/configuration.html

For log4j2, use configurationFile option:

java -Dlog4j.configurationFile=/path/to/log4j2.xml -jar ...

http://logging.apache.org/log4j/2.0/manual/configuration.html

独闯女儿国 2024-09-04 04:54:47

您是否尝试过 java -Dlog4j.configuration=/path/to/log4j.xml -jar

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

暗地喜欢 2024-09-04 04:54:47

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

myjar.jar 文件包含 log4j.default.configuration

您可以使用此参数运行程序来启动应用程序

java  -jar -Dlog4j.configuration=log4j.properties  target\yourfile-v01_000_01_c002.jar

示例代码

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

    }
}

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.

myjar.jar file contains 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());

    }
}
装纯掩盖桑 2024-09-04 04:54:47

java -cp "path/to/your/log4jxml:path/to/yourjar.jar" your.package.MainClass

目录 "path/to/your/log4jxml" 中的 log4j.xml 将覆盖 "path/to/your/log4jxml" 中的 log4j.xml 文件/to/yourjar.jar”。

请参阅在java类路径中设置多个jar

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".

Please refer to Setting multiple jars in java classpath.

忘年祭陌 2024-09-04 04:54:47

我在使用 log4j 与 Sun 的 JDK 和自动配置时遇到问题。

您可以

String filename = System.getProperty("log4j.configuration");
DOMConfigurator(filename);

在使用 Logger 之前使用它:

I had problems using log4j with Sun's JDK and automatic configuration.

You can use this:

String filename = System.getProperty("log4j.configuration");
DOMConfigurator(filename);

before using Logger.

单挑你×的.吻 2024-09-04 04:54:47

``log4j2最简单的路径配置是使用static blocksettinglog4j.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("");
    }
}

那么结构可以是:

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

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

The simplest path configuration for ``log4j2is using thestatic blocksettinglog4j.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

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

倒带 2024-09-04 04:54:47

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

"-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 found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文