简体   繁体   English

logback.xml:仅覆盖一个类的根级别

[英]logback.xml : overriding root level for one class only

Given the following root in logback.xml: 给出logback.xml中的以下根:

<root level="INFO">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
</root>

defining the following logging rule: 定义以下日志记录规则:

<logger name="com.myproject.mypackage.MyClass" level="DEBUG">
   <appender-ref ref="STDOUT" />
</logger>

The log level in myclass won't be DEBUG, because the root level is info. myclass中的日志级别不是DEBUG,因为根级别是info。 But I want to see debug logs in this particular class ONLY. 但是我想在这个特定的类中看到调试日志。 Is that achieveable without changing the root level (which would cause the app to vomit debug logs from all over the place)? 这是否可以在不改变根级别的情况下实现(这会导致应用程序从所有地方呕吐调试日志)?

EDIT: I tried this as well: 编辑:我也试过这个:

<logger name="com.myproject" level="INFO"/>
<logger name="com.myproject.mypackage.MyClass" level="DEBUG">

<root level="DEBUG">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
</root>

So the idea was to set the root to debug so everything is DEBUG, but set everything under 'com.myproject' to info so the log level really is INFO, but set MyClass to DEBUG. 所以我的想法是设置root来调试所以一切都是DEBUG,但是将'com.myproject'下的所有东西设置为info,所以日志级别确实是INFO,但是将MyClass设置为DEBUG。 It didn't work :( 它不起作用:(

"Root" level does not restrict levels of other loggers, it merely sets the default. “Root”级别不限制其他记录器的级别,它仅设置默认值。 So <root level="INFO"> and <logger name="some.name" level="DEBUG"> are perfectly suitable together, and you don't need to relax the "root" level. 所以<root level="INFO"><logger name="some.name" level="DEBUG">完全合适,你不需要放松“根”级别。 So both examples should log on debug level for logger named com.myproject.mypackage.MyClass . 因此,这两个示例都应该记录名为com.myproject.mypackage.MyClass记录器的调试级别。 So if your configuration logs something and does not log from com.myproject.mypackage.MyClass , the problem should be in another place. 因此,如果您的配置记录某些内容并且未从com.myproject.mypackage.MyClass记录,则问题应该在另一个地方。

Also, if you want some logger to log into its own appender only, as in the first example, you should use additivity="false" attribute on logger , otherwise it will log to both root appenders and logger-specific appenders. 此外,如果您希望某些记录器仅登录到自己的appender,如第一个示例所示,您应该在logger上使用additivity="false"属性,否则它将记录到根appender和特定于logger的appender。

For example: 例如:

import org.slf4j.LoggerFactory;

class Scratch {
    public static void main(String[] args) {
        LoggerFactory.getLogger("some.logger").info("info msg");
        LoggerFactory.getLogger("another.logger").info("info msg");
        LoggerFactory.getLogger("another.logger").error("error msg");
    }
}

logback.xml: logback.xml:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>STDOUT: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT2" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>STDOUT2: %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="some.logger" level="info" additivity="false">
        <appender-ref ref="STDOUT2" />
    </logger>

    <root level="error">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Result: 结果:

STDOUT2: 16:58:58.973 [main] INFO  some.logger - info msg
STDOUT: 16:58:58.979 [main] ERROR another.logger - error msg

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

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