简体   繁体   English

如何在 slf4j 记录器中启用调试?

[英]How to enable debug in slf4j Logger?

How to globally enable debug for all the slf4j.Logger objects?如何全局启用所有slf4j.Logger对象的debug

Programmatically, with logback:以编程方式,使用 logback:

setLoggingLevel(ch.qos.logback.classic.Level.DEBUG);

where在哪里

public static void setLoggingLevel(ch.qos.logback.classic.Level level) {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
    root.setLevel(level);
}

exists various capability to switch debug log on:存在切换调试登录的各种功能:
this article have good explanation all of those.这篇文章对所有这些都有很好的解释。 to me good fit is:对我来说合适的是:

Using slf4j with Log4j logger将 slf4j 与 Log4j 记录器一起使用
create file src/main/resources/log4j.properties创建文件src/main/resources/log4j.properties

log4j.rootLogger=DEBUG, STDOUT
log4j.logger.deng=INFO
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Use logback as the slf4j binding.使用 logback 作为 slf4j 绑定。

The default behaviour without an configuration file is to log all events at level DEBUG and above to System.out.没有配置文件的默认行为是将 DEBUG 及以上级别的所有事件记录到 System.out。 See http://logback.qos.ch/manual/configuration.html#automaticConf for details.有关详细信息,请参阅http://logback.qos.ch/manual/configuration.html#automaticConf

For log4j对于 log4j

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">

<!-- ============================== -->
<!-- Append messages to the console -->
<!-- ============================== -->

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <param name="Threshold" value="INFO"/>

    <layout class="org.apache.log4j.PatternLayout">
        <!-- The default pattern: Date Priority [Category] Message\n -->
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
    </layout>
</appender>

<appender name="web" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="Threshold" value="DEBUG"/>
    <param name="Append" value="true"/>
    <param name="File" value="${catalina.home}/logs/web.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
    </layout>
</appender>

<category name="com.idc.scd" additivity="false">
    <priority value="${log4j.category.com.mypackage}"/>
    <appender-ref ref="web"/>
</category>

</log4j:configuration>

by this cofiguration your all "com.mypackage" logs will be written in "web.log" file under catalina.home.通过这种配置,您的所有“com.mypackage”日志都将写入 catalina.home 下的“web.log”文件中。

Pass the System Property -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG at your Java startup for the SLF4J Simple api在 SLF4J Simple api 的 Java 启动时传递系统属性 -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG

在此处输入图片说明

取决于您使用的是什么绑定...如果它是 log4j,请查看http://logging.apache.org/log4j/1.2/manual.html及其配置章节

If you use log4j as the binding of slf4j you can crete a log4j.xml (or log4j.properties) file and add it to the classpath.如果您使用 log4j 作为 slf4j 的绑定,您可以创建一个 log4j.xml(或 log4j.properties)文件并将其添加到类路径中。 An example could be:一个例子可能是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
  <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <param name="Threshold" value="DEBUG" />
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{1}] %m%n" />
    </layout>
  </appender>
  <root>
    <appender-ref ref="CONSOLE" />
  </root>
</log4j:configuration>

Here's a sample configuration I usually use to setup logging with logback.这是我通常用于使用 logback 设置日志记录的示例配置。
Gradle dependencies (the same apply to Maven) Gradle 依赖项(同样适用于 Maven)

compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

logback.xml configuration to be placed in the project's classpath (src/main/resources) logback.xml配置要放在项目的类路径(src/main/resources)中

<configuration>

    <statusListener class="ch.qos.logback.core.status.NopStatusListener" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n</Pattern>
        </layout>
    </appender>
    
    <!-- enable debug only on org.hibernate.SQL package -->
    <logger name="org.hibernate.SQL" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

If you are using slf4j with springboot, you just need to set debug level in application.properties .如果你在 springboot 中使用 slf4j,你只需要在application.properties中设置调试级别。

logging.level.root=DEBUG

You also can set the specific part by setting logging.group .您也可以通过设置logging.group来设置特定部分。

Image that, if you have com.pxample , com.example , com.dxample in src/main/java/ and you setting:想象一下,如果您在src/main/java/中有com.pxamplecom.examplecom.dxample并且您设置:

logging.group.mycustomgroup=com.pxample, com.example
logging.level.mycustomgroup=DEBUG

, then only com.pxample , com.example will show debug output for you. ,然后只有com.pxamplecom.example将为您显示调试 output 。

Just add the following to the application.properties只需将以下内容添加到application.properties

logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.context=DEBUG

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

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