How to enable debug in slf4j Logger?

2020-02-18 04:12发布

How to globally enable debug for all the slf4j.Logger objects?

6条回答
够拽才男人
2楼-- · 2020-02-18 04:48

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
create file 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
查看更多
一夜七次
3楼-- · 2020-02-18 04:50

Use logback as the slf4j binding.

The default behaviour without an configuration file is to log all events at level DEBUG and above to System.out. See http://logback.qos.ch/manual/configuration.html#automaticConf for details.

查看更多
Summer. ? 凉城
4楼-- · 2020-02-18 04:52

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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-02-18 05:01

depends on what binding you are using... if e.g. it's log4j have a look at http://logging.apache.org/log4j/1.2/manual.html and its Configuration chapter

查看更多
何必那么认真
6楼-- · 2020-02-18 05:03

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. 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>
查看更多
Emotional °昔
7楼-- · 2020-02-18 05:07

Programmatically, with 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);
}
查看更多
登录 后发表回答