简体   繁体   English

如何检查 log4j.properties 是否已加载?

[英]How to check if log4j.properties is loaded?

I'm using log4j (compile group slf4j, runtime group logback) with configuration in log4j.properties file.我正在使用 log4j(编译组 slf4j,运行时组 logback)和 log4j.properties 文件中的配置。

I think I configured it properly with this options我想我用这个选项正确配置了它

log4j.rootLogger=INFO, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.stdout.threshold=info

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/user/logging.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=20
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.file.threshold=info

But when I run debug message like但是当我运行调试消息时

logger.debug("foo")

I see output in Console which doesn't match my pattern.我在控制台中看到与我的模式不匹配的输出。

Next problem is that nothing is in file (/home/user/logging.log) so I think my properties file is not loaded and don't know how to repair it.下一个问题是文件 (/home/user/logging.log) 中没有任何内容,因此我认为我的属性文件未加载并且不知道如何修复它。

I put my log4j.properties file in root level of my eclipse project, in resources (src/main/resources) but still is nothing changed.我将我的 log4j.properties 文件放在我的 eclipse 项目的根级别,在资源 (src/main/resources) 中,但仍然没有任何改变。 I have my Project folder in classpath properly.我的项目文件夹在类路径中正确。

EDIT: The problem was that I'm using Logback ( http://logback.qos.ch/ ) as my runtime Logger, but this library using logback.xml config file, not log4j.properties with org.apache.log4j library.编辑:问题是我使用Logback ( http://logback.qos.ch/ ) 作为我的运行时记录器,但是这个库使用logback.xml配置文件,而不是log4j.propertiesorg.apache.log4j库。

As per the logging level INFO < DEBUG that's why logger.debug("foo") will not be logged. 根据日志记录级别INFO < DEBUG ,这就是为什么不记录logger.debug("foo")的原因。

This is the logging level by order most specific to least 这是最具特定至最低顺序的日志记录级别

DEBUG > INFO > WARN > ERROR > FATAL

If INFO is enabled then logging level below it will also be enabled. 如果启用了INFO ,则还将启用其下方的日志记录级别。

Try 尝试

log4j.rootLogger=DEBUG, stdout, file
...
log4j.appender.stdout.threshold=debug
...
log4j.appender.file.threshold=debug

在此输入图像描述

To answer the actual question,要回答实际问题,

How to check if log4j.properties is loaded?如何检查 log4j.properties 是否已加载?

log4j's internal debugging can be enabled by setting the log4j.debug system property.可以通过设置 log4j.debug 系统属性来启用 log4j 的内部调试。 During startup, log4j logs shows from where and how the configuration is loaded.在启动期间,log4j 日志会显示配置的加载位置和方式。

For example,例如,

java -Dlog4j.debug -Dlog4j.configuration=file:/path/to/log4j.properties -jar ...

Here, when -Dlog4j.debug is added in VM arguments, the following console logs are generated during startup.这里,当在 VM 参数中添加 -Dlog4j.debug 时,在启动过程中会生成以下控制台日志。

log4j: Using URL [file:C:/<...PATH..removed...>/resources/test_logging.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:C:/<...PATH..removed...>/resources/test_logging.properties
log4j: Parsing for [root] with value=[DEBUG, console].
log4j: Level token is [DEBUG].
log4j: Category root set to DEBUG
log4j: Parsing appender named "console".
log4j: Parsing layout options for "console".
log4j: Setting property [conversionPattern] to [%d DmccClient[%X{PID}] :%t: %c:%L %-4p - %m%n].
log4j: End of parsing for "console".
log4j: Setting property [immediateFlush] to [true].
log4j: Setting property [target] to [System.out].
log4j: Setting property [encoding] to [UTF-8].
log4j: Parsed "console" options.
log4j: Level token is [FINEST#org.apache.log4j.helpers.UtilLoggingLevel].
log4j: toLevel:class=[org.apache.log4j.helpers.UtilLoggingLevel]:pri=[FINEST]
log4j: Parsing appender named "console".
log4j: Appender "console" was already parsed.
log4j: Finished configuring.

Please See also: The answer to this question question helped to find the answer.请参见:答案这个问题的问题帮助找到答案。

I'm using log4j (compile group slf4j, runtime group logback)我正在使用 log4j(编译组 slf4j,运行时组 logback)

Actually, if those details are accurate you are NOT using log4j.实际上,如果这些细节是准确的,那么您就没有使用 log4j。 Logback is a different logging package to log4j, and slf4j is a facade that is intended to hide the logging details from (agnostic) application code. Logback 是一个log4j不同的日志包,而 slf4j 是一个外观,旨在向(不可知的)应用程序代码隐藏日志细节。

So ... you are actually using logback for your logging.所以......您实际上是在使用 logback 进行日志记录。 Hence you need to use a logback compatible logging config file.因此你需要使用一个 logback 兼容的日志配置文件。 You can find details in:您可以在以下位置找到详细信息:

Note that logback configuration files are XML.请注意,logback 配置文件是 XML。

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

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