Java 如何确定 Logback 实际使用的日志配置源?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18922730/
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
How can I determine what log configuration source Logback actually used?
提问by Peter Mularien
log4j has a property, log4j.debug
, which will helpfully provide the user with an indication of which configuration file was actually used to configure the logging system.
log4j 有一个属性,log4j.debug
,它将帮助用户指示实际使用哪个配置文件来配置日志系统。
I haven't been able to find anything equivalent with the (otherwise superior) Logback logging framework. Is there any way to print (for diagnostic purposes) at runtime, which configuration file Logback used to bootstrap itself?
我一直无法找到与(否则更高级)Logback 日志框架等效的任何内容。有没有办法在运行时打印(用于诊断目的)Logback 用于引导自身的配置文件?
[edit] To clarify, I'd ideally like a solution that doesn't require me to modify the configuration file itself (since a badly assembled third-party JAR, for example, may be picked up incorrectly, and prior to my logback configuration XML).
[编辑] 澄清一下,我理想情况下喜欢一个不需要我修改配置文件本身的解决方案(例如,因为一个组装不当的第三方 JAR 可能会被错误地拾取,并且在我的 logback 配置之前XML)。
采纳答案by Michael R
You can set a Java system property to output Logback debugging info:
你可以设置一个 Java 系统属性来输出 Logback 调试信息:
java -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener
This is further explained by the Logback documentation for automatic status printing(very bottom mentions forcing status output) and the logback.statusListenerClass property:
自动状态打印的 Logback 文档(最底部提到强制状态输出)和logback.statusListenerClass 属性进一步解释了这一点:
In the absence of status messages, tracking down a rogue logback.xml configuration file can be difficult, especially in production where the application source cannot be easily modified. To help identify the location of a rogue configuration file, you can set a StatusListener via the "logback.statusListenerClass" system property (defined below) to force output of status messages. The "logback.statusListenerClass" system property can also be used to silence output automatically generated in case of errors.
在没有状态消息的情况下,跟踪恶意 logback.xml 配置文件可能很困难,尤其是在应用程序源无法轻松修改的生产环境中。为了帮助识别流氓配置文件的位置,您可以通过“logback.statusListenerClass”系统属性(定义如下)设置 StatusListener 以强制输出状态消息。“logback.statusListenerClass”系统属性也可用于在出现错误时自动生成静音输出。
回答by Frederic Close
you can set debug="true"
in a logback.xml
file that you control like this:
您可以debug="true"
在logback.xml
您控制的文件中进行设置,如下所示:
<configuration debug="true">
(...)
</configuration
and tho make sure that file is going to be used by logback add following VM argument when you start your program:
并确保在启动程序时 logback 将使用该文件添加以下 VM 参数:
-Dlogback.configurationFile=/path/to/yourlogback.xml
This does not really answer to your question but gives you a work around solution.
这并不能真正回答您的问题,而是为您提供了解决方案。
回答by Sotirios Delimanolis
If you want to go deepinto Logback
, you can do the following
如果你想去深成Logback
,你可以做以下
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws Exception {
LoggerContext loggerContext = ((ch.qos.logback.classic.Logger)logger).getLoggerContext();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);
System.out.println(mainURL);
// or even
logger.info("Logback used '{}' as the configuration file.", mainURL);
}
}
It will print the URL
of the loaded configuration file.
它将打印URL
加载的配置文件。
回答by Kenny Cason
Not very scientific, but it works if you just want a quick confirmation.
不是很科学,但如果您只想快速确认,它会起作用。
I simply changed the log entry pattern and observed whether or not it changed in my console/log file.
我只是更改了日志条目模式并观察它是否在我的控制台/日志文件中发生了变化。