java 如何在运行时覆盖 jar 中的 log4j 属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14527759/
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-10-31 16:33:20  来源:igfitidea点击:

How can I override the log4j properties file in my jar at runtime

javaunixjarlog4j

提问by user1741202

Possible Duplicate:
Dynamically Changing log4j log level

可能的重复:
动态更改 log4j 日志级别

I have a jarfile with a log4jproperties file included. I tried to use

我有一个包含log4j属性文件的jar文件。我试着用

-Dlog4j.configuration=file:[filename]

-Dlog4j.configuration=file:[filename]

to change the properties file at runtimeusing java, and it didn't work. I need to do this in a shfile Can anyone please tell me how to this? I also tried adding

运行时使用 java更改属性文件,但它不起作用。我需要在sh文件中执行此操作 谁能告诉我如何执行此操作?我也尝试添加

-Dlog4j.defaultInitOverride=true

回答by Jintian DENG

It's possible to override the log4j properties file at runtime. You may fall into this situation if you cann't :

可以在运行时覆盖 log4j 属性文件。如果你不能,你可能会陷入这种情况:

  1. In your jar or somewhere in the code, the log4j system is initialised manually, e.g.

     URL url=loader.getResource(LOGGER_CONFIG_FILE)
     DOMConfigurator.configure(url);
    
  1. 在您的 jar 或代码中的某个地方,log4j 系统是手动初始化的,例如

     URL url=loader.getResource(LOGGER_CONFIG_FILE)
     DOMConfigurator.configure(url);
    

回答by paulsm4

Here's a good,step-by-step tutorial (with good screenshots!) that shows a couple of different alternatives for changing your log4j settings at runtime:

这是一个很好的分步教程(带有很好的屏幕截图!),它显示了在运行时更改 log4j 设置的几种不同替代方法: