Java log4j 在哪里/如何查找 log4j.properties 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2716824/
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
Where/how does log4j look for a log4j.properties file?
提问by Marplesoft
I'm trying out log4j in a simple test app. I create a new Java project in eclipse and add the log4j JAR (v1.2.16) to my build path. I then create a simple class that prints Hello World. Then I use the log4j Logger
class to log a info message. When I run the app, I see the log message, using what I assume is the default appender and layout. Great. What I'm having trouble with is adding my own configuration. This is what I've done:
我正在一个简单的测试应用程序中试用 log4j。我在 eclipse 中创建了一个新的 Java 项目,并将 log4j JAR (v1.2.16) 添加到我的构建路径中。然后我创建了一个打印 Hello World 的简单类。然后我使用 log4jLogger
类来记录信息消息。当我运行应用程序时,我看到日志消息,使用我假设的默认附加程序和布局。伟大的。我遇到的问题是添加我自己的配置。这就是我所做的:
Created a log4j.properties file with a custom appender and log level and placed it into the src folder (which upon compilation gets copied to the bin folder). Run the app - no change.
创建了一个带有自定义附加程序和日志级别的 log4j.properties 文件,并将其放入 src 文件夹(编译时将其复制到 bin 文件夹)。运行应用程序 - 没有变化。
I try adding PropertyConfigurator.configure("log4j.properties")
. Run the app - no change. No errors, but no change.
我尝试添加PropertyConfigurator.configure("log4j.properties")
. 运行应用程序 - 没有变化。没有错误,但没有变化。
What do I have to do to get log4j to load my configuration file?
我该怎么做才能让 log4j 加载我的配置文件?
采纳答案by Marplesoft
Argh. I discovered the problem was that eclipse had imported the wrong Logger
class. It had imported java.util.logging.Logger which of course has it's own configuration that is different from log4j. Oh well, hope somebody else does this and gets it solved by reading this question.
啊。我发现问题是 eclipse 导入了错误的Logger
类。它导入了 java.util.logging.Logger ,当然它有自己的配置,与 log4j 不同。哦,好吧,希望其他人这样做并通过阅读这个问题来解决它。
回答by Jason Day
You can enable log4j internal debugging by setting the log4j.debug
system property. Among other things, this will cause log4j to show how it is configuring itself.
您可以通过设置log4j.debug
系统属性来启用 log4j 内部调试。除此之外,这将导致 log4j 显示它如何配置自身。
You can try explicitly setting the URL to the configuration file with the log4j.configuration
system property.
您可以尝试使用log4j.configuration
系统属性显式设置配置文件的 URL 。
See also: this question.
另见:这个问题。
回答by leonbloy
log4j.properties
should be in your classpath. The "src folder" which is copied to the "bin folder" (I assume you are speaking of a Eclipse setup here), normally belongs to your classpath, so it should be found (are you placing it at the top of the "src" folder, right?)
log4j.properties
应该在你的类路径中。复制到“bin 文件夹”的“src 文件夹”(我假设您在这里说的是 Eclipse 设置),通常属于您的类路径,因此应该可以找到它(您是否将它放在“src " 文件夹,对吗?)
回答by crowne
Look in the manual under the heading Default Initialization Procedure, where you'll find the following:
查看“默认初始化程序”标题下的手册,您将在其中找到以下内容:
The exact default initialization algorithm is defined as follows:
- Setting the
log4j.defaultInitOverride
system property to any other value than "false
" will cause log4j to skip the default initialization procedure (this procedure).- Set the resource string variable to the value of the
log4j.configuration
system property. The preferred way to specify the default initialization file is through thelog4j.configuration
system property. In case the system property log4j.configuration is not defined, then set the string variable resource to its default value"log4j.properties
".- Attempt to convert the resource variable to a URL.
- If the resource variable cannot be converted to a URL, for example due to a MalformedURLException, then search for the resource from the classpath by calling org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) which returns a URL. Note that the string "
log4j.properties
" constitutes a malformed URL.See Loader.getResource(java.lang.String)for the list of searched locations.- If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL. The PropertyConfiguratorwill be used to parse the URL to configure log4j unless the URL ends with the "
.xml
" extension, in which case the DOMConfiguratorwill be used. You can optionaly specify a custom configurator. The value of thelog4j.configuratorClass
system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement theConfigurator
interface.
确切的默认初始化算法定义如下:
- 将
log4j.defaultInitOverride
系统属性设置为“false
”以外的任何其他值将导致 log4j 跳过默认初始化过程(此过程)。- 将资源字符串变量设置为
log4j.configuration
系统属性的值。指定默认初始化文件的首选方法是通过log4j.configuration
系统属性。如果系统属性 log4j.configuration 未定义,则将字符串变量资源设置为其默认值“log4j.properties
”。- 尝试将资源变量转换为 URL。
- 如果资源变量无法转换为 URL,例如由于 MalformedURLException,则通过调用返回 URL 的 org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) 从类路径中搜索资源. 请注意,字符串 "
log4j.properties
" 构成了格式错误的 URL。有关 搜索位置的列表,请参阅 Loader.getResource(java.lang.String)。- 如果找不到 URL,则中止默认初始化。否则,从 URL 配置 log4j。的 PropertyConfigurator将用于解析URL配置Log4j除非使用“的URL端
.xml
在这种情况下,”扩展, DOMConfigurator将被使用。您可以选择指定自定义配置器。该值log4j.configuratorClass
系统属性作为您的自定义配置的完全限定类名。您指定的自定义配置器必须实现该Configurator
接口。
回答by Peter Van geenhoven
I know this is a couple of months old, but I feel the need to point out that the scr folder isn't "copied" to the bin folder, nor is it part of your runtime classpath....(build path is not runtime classpath!). Eclipse compiles the source files in the src folder to the bin (or whatever you like) folder. It's the bin folder that is part of your runtime classpath.
我知道这已经有几个月了,但我觉得有必要指出 scr 文件夹没有“复制”到 bin 文件夹,也不是运行时类路径的一部分......(构建路径不是运行时类路径!)。Eclipse 将 src 文件夹中的源文件编译到 bin(或任何您喜欢的)文件夹中。bin 文件夹是运行时类路径的一部分。
Just wanted to point this out as these threads are often read by very junior programmers as well, and I'm always frustrated that most of them don't grasp the finesse of the Java classpath, and hence make avoidable mistakes against it.
只是想指出这一点,因为这些线程也经常被非常初级的程序员阅读,我总是很沮丧,因为他们中的大多数人没有掌握 Java 类路径的技巧,因此对它犯了可以避免的错误。
回答by user2029861
The problem may be in the classpath
, if the classpath
was defined.
问题可能出在 中classpath
,如果classpath
已定义。
The reason it wasn't loading (in my case): There was a conflicting log4j.properties
file in one of my jars, and it was overloading the one in my classpath
.
它没有加载的原因(在我的情况下):log4j.properties
我的一个 jar 中有一个冲突的文件,并且它在我的classpath
.
In short, if your log4j.properties
file isn't loading, there might be another one somewhere else overriding it.
简而言之,如果您的log4j.properties
文件未加载,则其他地方可能有另一个文件覆盖它。
Just thought I'd throw this in too, in case anyone else runs into this. I just spent the last 5 hours trying to figure out why my default log4j.properties
wouldn't load.
只是想我也会把它扔进去,以防其他人遇到这个。我只是花了最后 5 个小时试图弄清楚为什么我的默认设置log4j.properties
无法加载。