在调试中运行 Logback

发布于 2024-09-24 21:25:28 字数 211 浏览 0 评论 0原文

我最近从 log4j 切换到 logback,想知道是否有一种简单的方法可以在调试模式下运行 logback,类似于 log4j 的 log4j.debug 属性。我需要查看它从哪里获取我的 logback.xml

文档提到使用 StatusPrinter 打印 logback 的内部状态,但这需要更改代码。

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.

The docs mention using a StatusPrinter to print out logback's internal status, but that would require code changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

☆獨立☆ 2024-10-01 21:25:28

[编辑]

这已在 Logback 1.0.4 中修复。您现在可以使用 -Dlogback.debug=true 来启用 logback 设置的调试。

-- 旧答案 --

不幸的是,没有办法通过系统属性启用调试。您必须在 logback.xml 中使用 。请提交功能请求。

[EDIT]

This has been fixed in Logback 1.0.4. You can now use -Dlogback.debug=true to enable debugging of the logback setup.

-- 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.

猫九 2024-10-01 21:25:28

我就是这样做的。我设置了一个名为“log.level”的系统属性,然后在 logback.xml 中引用它。

编辑:缺点是您必须始终设置“log.level”。我处理这个问题的方法是检查我的 main 方法并将其设置为 INFO(如果尚未设置),请务必在首次记录调用之前执行此操作。然后我可以在命令行上覆盖,并有一个合理的默认值。

这是它在我的 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>

This is how I do it. I set a system property called 'log.level', then I reference it in 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.

Here is how it looks in my 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>
星星的轨迹 2024-10-01 21:25:28

我无法使用所选的答案使其工作。但是,以下方法有效:

java -Dlogback.configurationFile=/path/to/config-debug.xml com.domain.Main

只需在服务器上的某个位置添加一个文件(本例中为 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>

使用前面提到的 -D 参数运行您的应用程序。

当一切恢复正常后,删除 -D 参数并重新启动应用程序。

来源:第 3 章:Logback 配置

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.

<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.

When things are back to normal, remove the -D parameter and restart your application.

Source: Chapter 3: Logback configuration

人间不值得 2024-10-01 21:25:28

您可以通过系统属性设置状态监听器类:

java -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener ...

请参阅:Logback手册

You can set the status listener class via system property:

java -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener ...

See: Logback manual

七度光 2024-10-01 21:25:28

嗯,这很容易。 使用

log.level = debug

您可以在 Spring boot 的 application.properties 中

。或者你也可以在logback.xml的配置文件中设置

<root level="${log.level}">
    <appender-ref ref="ANY_APPENDER" />
</root>

Well, It's pretty easy. Either you can use

log.level = debug

inside the application.properties of Spring boot.

or you can also set this in the configuration file of logback.xml

<root level="${log.level}">
    <appender-ref ref="ANY_APPENDER" />
</root>
栀梦 2024-10-01 21:25:28

在 Eclipse 中你可以有多个运行配置。打开你的主课。转到 Eclipse 工具栏上的“调试”下拉菜单,然后选择“调试配置”。单击左上角的新启动配置图标。为您的启动配置指定一个更好的名称。单击名称下方的“参数”选项卡,然后输入 -Dlog.level=debug 或任何您想要的内容。单击关闭或调试

您可以再次执行此操作并指定 -Dlog.level=warn 例如。

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

You can do this again and specify -Dlog.level=warn for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文