Log4j 初始化时出现警告?
我正在尝试了解 log4j 所以我只是尝试做一些非常简单的事情;
Logger logger = Logger.getLogger("ClientApplicationLog");
logger.info("Logger Test");
但做了这个之后我得到了;
log4j:WARN No appenders could be found for logger (ClientApplicationLog).
log4j:WARN Please initialize the log4j system properly.
你知道我错在哪里吗?
谢谢大家
I am trying to learn about log4j so I just tried to do something which is very simple;
Logger logger = Logger.getLogger("ClientApplicationLog");
logger.info("Logger Test");
But after making this I got;
log4j:WARN No appenders could be found for logger (ClientApplicationLog).
log4j:WARN Please initialize the log4j system properly.
Do you know where I am wrong ?
Thank you all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的类路径中缺少
log4j.properties
或log4j.xml
。您可以使用以下方法绕过此问题,
但请注意,这只会登录到 System.out,因此不建议这样做。您确实应该使用上面的文件之一并写入日志文件。
log4j.properties
的一个非常简单的示例是You're missing the
log4j.properties
orlog4j.xml
in your classpath.You can bypass this by using
But beware this will ONLY log to System.out and is not recommended. You should really use one of the files above and write to a log file.
A very simple example of
log4j.properties
would be您需要在类路径中的某个位置有一个 log4j.xml,其中包含告诉它在哪里记录、如何记录等信息。或者,您可以在代码中以编程方式设置所有这些,但在实现中具有灵活性会更好。
我的 log4j.xml 如下所示:
You need to have a log4j.xml somewhere in your class path with information telling it where to log, how to log etc. Alternatively you can set all this programmatically in your code, but it's much nicer to have the flexibility in your implementation.
My log4j.xml looks like:
这只是警告。
修复
当找不到默认配置文件
log4j.properties
和log4j.xml
并且应用程序未执行显式配置时,会出现此问题。要解决此问题,只需创建/复制
log4j.properties
或log4j.xml
到类路径上的某个位置(通常与 jar 文件相同)。(可选)设置 java 选项:
-Dlog4j.configuration=file:///path/to/log4j.properties
。调试
为了调试,您可以尝试使用
-Dlog4j.debug=true
参数。log4j.properties
的配置log4j.properties
的示例配置:这是另一个使用多个附加程序的配置文件:
Apache Solr
如果使用 Solr,则复制
; /example/resources/log4j.properties
到类路径上的某个位置。Solr 的
log4j.properties
示例配置如下:另请参阅:
This is just the warning.
Fixing
This occurs when the default configuration files
log4j.properties
andlog4j.xml
can not be found and the application performs no explicit configuration.To fix that, simply create/copy
log4j.properties
orlog4j.xml
into your a location on the classpath (usually the same as the jar files).Optionally set java option:
-Dlog4j.configuration=file:///path/to/log4j.properties
.Debugging
For debugging, you may try to use
-Dlog4j.debug=true
parameter.Configuration of
log4j.properties
Sample configuration of
log4j.properties
:Here is another configuration file that uses multiple appenders:
Apache Solr
If using Solr, copy
<solr>/example/resources/log4j.properties
into a location on the classpath.Sample configuration of
log4j.properties
from Solr goes like:See also:
您需要为您的记录器(例如日志文件)定义一个附加程序,例如在类路径上的 log4j.properties 文件中。
本教程应该包含您需要了解的所有内容。
You need to define an appender for your logger (e.g. a log file), e.g. in a log4j.properties files on the classpath.
This tutorial should contain everything you need to know.
似乎您的 log4j.properties 不在类路径中。确保它是,并且在您的配置文件中定义了一个名为“ClientApplicationLog”的记录器。
Seems like your log4j.properties is not in the classpath. Make sure it is and that within your config-file you have a logger with the name "ClientApplicationLog" defined.