调试 Spring 配置不起作用

Debugging Spring Configuration doesn't work

我在 5 月 Spring 应用程序中添加了安全问题,所以我想调试 Spring 到 log4j

Log4j 正在运行,因为我的服务正在将调试信息记录到控制台。 但是 Spring 没有向我的控制台记录任何内容

这是我的 log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MM/dd/yy - HH\:mm\:ss,SSS} [%t] %-5p %c %x - %m%n" />
        </layout>
    </appender>
    <logger name="org.springframework" additivity="false">
        <level value="DEBUG"/>
        <appender-ref ref="CA"/>
    </logger>   
    <logger name="com.test" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="CA"/>
    </logger>
</log4j:configuration>

我在服务器启动选项中加入 --Dlog4j.debug 时考虑到了这一点。结果:

log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework] additivity to [false].
log4j: Level value for org.springframework is  [DEBUG].
log4j: org.springframework level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{MM/dd/yy - HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n].
log4j: Adding appender named [CA] to category [org.springframework].
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.test] additivity to [false].
log4j: Level value for com.testis  [INFO].
log4j: com.testlevel set to INFO
log4j: Adding appender named [CA] to category [com.test].
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework] additivity to [false].
log4j: Level value for org.springframework is  [DEBUG].
log4j: org.springframework level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{MM/dd/yy - HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n].
log4j: Adding appender named [CA] to category [org.springframework].
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.test] additivity to [false].
log4j: Level value for com.test is  [INFO].
log4j: com.test level set to INFO
log4j: Adding appender named [CA] to category [com.test].

我尝试按照 question

中所述将其添加到我的 web.xml 中
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

我在其他应用程序中使用了这个调试,我真的没有看到原因... 感谢您的帮助

找到了!

使用 -Dorg.apache.commons.logging.diagnostics.dest=STDOUT 您可以调试 commons-logging 配置。

我发现我的依赖项中的一个 jar (jcl-over-slf4j-1.5.8.jar) 有一个 META-INF/services/org.apache.commons.logging.LogFactory 定义了 SLF4J 的记录器。

所以添加一个-Dorg。apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl,我将记录器恢复为log4j。