Java 在调试中运行 Logback
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3802054/
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
Run Logback in Debug
提问by dogbane
I've recently switched from log4j to logback and am wondering if there is an easy way to run logback in debug mode, similar to log4j's log4j.debug
property. I need to see where it is picking up my logback.xml
from.
我最近从 log4j 切换到 logback,想知道是否有一种简单的方法可以在调试模式下运行 logback,类似于 log4j 的log4j.debug
属性。我需要看看它是logback.xml
从哪里接我的。
The docs mention using a StatusPrinter
to print out logback's internal status, but that would require code changes.
文档提到使用 aStatusPrinter
来打印 logback 的内部状态,但这需要更改代码。
采纳答案by Aaron Digulla
[EDIT]
[编辑]
This has been fixed in Logback 1.0.4. You can now use -Dlogback.debug=true
to enable debugging of the logback setup.
这已在 Logback 1.0.4 中修复。您现在可以-Dlogback.debug=true
用于启用 logback 设置的调试。
-- Old Answer --
-- 旧答案 --
Unfortunately, there is no way to enable debugging via a System property. You have to use <configuration debug="true">
in the logback.xml
. Please submit a feature request.
不幸的是,无法通过系统属性启用调试。您必须<configuration debug="true">
在logback.xml
. 请提交功能请求。
回答by David Roussel
This is how I do it. I set a system property called 'log.level', then I reference it in logback.xml.
我就是这样做的。我设置了一个名为“log.level”的系统属性,然后在 logback.xml 中引用它。
Edit: The downside is that you MUST have 'log.level' always set. The way I deal with this is to check in my main method and set it to INFO if not already set, be sure to do this before you first logging calls. Then I can override on the command line, and have a sensible default.
编辑:缺点是您必须始终设置“log.level”。我处理这个问题的方法是检查我的主要方法并将其设置为 INFO 如果尚未设置,请务必在第一次记录调用之前执行此操作。然后我可以在命令行上覆盖,并有一个合理的默认值。
Here is how it looks in my logback.xml:
这是它在我的 logback.xml 中的样子:
<configuration>
<logger name="com.mycompany.project" level="${log.level}" />
<logger name="httpclient" level="WARN" />
<logger name="org.apache" level="WARN" />
<logger name="org.hibernate" level="WARN" />
<logger name="org.hibernate.cfg.AnnotationBinder" level="WARN" />
<logger name="org.hibernate.cfg.annotations" level="WARN" />
<logger name="org.quartz" level="WARN" />
<logger name="org.springframework" level="WARN" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-16thread] %-5level %-35.35logger{30} - %msg%n</pattern>
</encoder>
</appender>
<root level="${log.level:-INFO}">
<appender-ref ref="STDOUT" />
</root>
</configuration>
回答by AixNPanes
In eclipse you can have multiple run configurations. Open your main class. Go to Debug dropdown on eclipse toolbar and select Debug configurations. Click the New launch configuration icon at the top left. Give your launch configuration a better name. Click the Arguments tab under the name and enter -Dlog.level=debug or whatever you want. Click Close or Debug
在 Eclipse 中,您可以有多个运行配置。打开你的主类。转到 Eclipse 工具栏上的调试下拉菜单,然后选择调试配置。单击左上角的新建启动配置图标。给你的启动配置一个更好的名字。单击名称下的 Arguments 选项卡并输入 -Dlog.level=debug 或您想要的任何内容。单击关闭或调试
You can do this again and specify -Dlog.level=warn for example.
您可以再次执行此操作并指定 -Dlog.level=warn 例如。
回答by Andres
I could not make it work using the chosen answer. However, the following worked:
我无法使用所选的答案使其工作。但是,以下方法有效:
java -Dlogback.configurationFile=/path/to/config-debug.xml com.domain.Main
Just add a file (config-debug.xml
in this example) somewhere on your server and leave it there when you need to debug. Like the following.
只需config-debug.xml
在服务器上的某处添加一个文件(在本例中),并在需要调试时将其保留在那里。像下面这样。
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{dd-MMM-yyyy HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Run your application using the afore mentioned -D
parameter.
使用上述-D
参数运行您的应用程序。
When things are back to normal, remove the -D
parameter and restart your application.
当一切恢复正常时,删除-D
参数并重新启动您的应用程序。
Source: Chapter 3: Logback configuration
回答by deve
You can set the status listener class via system property:
您可以通过系统属性设置状态侦听器类:
java -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener ...
See: Logback manual
请参阅:登录手册