简体   繁体   English

外部 log4j 配置文件未从可执行文件 jar 加载

[英]External log4j configuration file not loaded from executable jar

I need to import my log4j2 configuration file at runtime once the user gives a parameter pointing to the path where the configuration file is located (I cannot use the -Dlog4j.configurationFile parameter).一旦用户提供指向配置文件所在路径的参数(我不能使用 -Dlog4j.configurationFile 参数),我需要在运行时导入我的 log4j2 配置文件。

All works fine when the code is executed inside Eclipse IDE, the configuration is correctly loaded from the external file, passed as parameter by the user.当代码在 Eclipse IDE 中执行时,一切正常,配置从外部文件正确加载,由用户作为参数传递。 Once i build the jar file, the same piece of code does not work and configuration of the external file is not correctly loaded for my logs.一旦我构建了 jar 文件,同一段代码就不起作用,并且没有为我的日志正确加载外部文件的配置。

The path to the log file is given as absolute path.日志文件的路径以绝对路径的形式给出。

Log4j2 version: 2.11 Log4j2 版本:2.11

Here is the code that i'm currently using:这是我目前正在使用的代码:

 public static void main(String[] args) {
                    LoggerContext context = (LoggerContext) LogManager.getContext(false);
                    File file = new File(args[0]);
                    context.setConfigLocation(file.toURI());
                    .....................
        }

args[0] contains the absolute path to the log4j2 configuration file. args[0] 包含 log4j2 配置文件的绝对路径。

Any idea of what i am doing wrong?知道我做错了什么吗? Thank you谢谢

I would suggest you try我建议你试试

File file = new File(args[0]);
LoggerContext context = Configurator.initialize("MyApp", null, file.toURI());

or just要不就

loggerContext context = Configurator.iniitalize("MyApp", null, args[0]);

Are you loading a properties file or XML file.您是在加载属性文件还是 XML 文件。 I had issues with properties file, but the XML one worked fine我的属性文件有问题,但是 XML 一个工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM