java 未从 jar 中提取 log4j.properties

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

log4j.properties not picked up from jar

javalogginglog4j

提问by harsh

I have a jar in the classpath of my server which contains the log4j.properties file on root of the jar for logging of the code. When the code in the jar runs the logging for the code is not occuring. When I analyzed the problem using java -Dlog4j.debugoption I got to know that the log4j is loading the log4j.properties file of axis-ant.jar(this also is on my classpath) and using it instead of my jar's property file.

我的服务器的类路径中有一个 jar,它包含 jar 根目录下的 log4j.properties 文件,用于记录代码。当 jar 中的代码运行时,代码的日志记录不会发生。当我使用java -Dlog4j.debug选项分析问题时,我知道 log4j 正在加载axis-ant.jar的 log4j.properties 文件(这也在我的类路径中)并使用它而不是我的 jar 的属性文件。

When i subsequently removed the axis-ant.jar from the classpath my jars log4j.properties file was picked immediately and logging for my code occured.

当我随后从类路径中删除 axis-ant.jar 时,我的 jars log4j.properties 文件立即被选中并记录了我的代码。

Can somebody please explain why is my log4j.properties fle not being loaded while axis-ant.jar is present?

有人可以解释为什么我的 log4j.properties 文件在axis-ant.jar 存在时没有被加载吗?

Surprisingly the same jar with axis-ant.jar present in classpath is running with proper logging successfully in another server.. both the server have the same java 5 version installed and same version of log4j jar

令人惊讶的是,类路径中存在的带有axis-ant.jar 的相同jar 正在另一台服务器上成功运行并正确记录。两台服务器都安装了相同的java 5 版本和相同版本的log4j jar

回答by FrVaBe

You should have a look at the log4jmanual. The section "Default Initialization Procedure" describes how log4j will try to find the initialization file and explains possibilities to match a special configuration (e.g. by setting the system property log4j.configuration).

你应该看看log4j手册。“默认初始化过程”一节描述了 log4j 将如何尝试查找初始化文件并解释匹配特殊配置的可能性(例如,通过设置系统属性log4j.configuration)。

If you do not want to do any special configuration you have to ensure that your configuration file will be the first one found on the classpath.

如果你不想做任何特殊的配置,你必须确保你的配置文件是在类路径中找到的第一个文件。

回答by Pushkar

The properties file is picked up from the jar based on the order in which the jar is found in classpath by your application server. Now there is no way to conclusively tell the jar from which the properties file is to be loaded.

根据应用程序服务器在类路径中找到 jar 的顺序,从 jar 中提取属性文件。现在没有办法最终告诉要从哪个 jar 加载属性文件。

To get around this problem most projects include your log4j.properties file in the server classpath itself. This way this file is the first properties file to be loaded and this is the one which your logging code gets.

为了解决这个问题,大多数项目在服务器类路径本身中包含您的 log4j.properties 文件。这样这个文件是第一个要加载的属性文件,这是您的日志代码获取的文件。

回答by Thomas

I'm note quite sure but it could be an order problem. Whichever log4j.properties is loaded first/last would win (I'd have to check that).

我很确定,但这可能是订单问题。无论哪个 log4j.properties 首先/最后加载都会获胜(我必须检查一下)。

回答by Suraj Chandran

Possibly because axis-ant.jar comes before in classpath the your own jar. I assume putting your jar before in classpath then axis-ant.jar should help

可能是因为axis-ant.jar 在类路径中出现在您自己的jar 之前。我假设将你的 jar 放在 classpath 之前然后 axis-ant.jar 应该有帮助