简体   繁体   English

使用spring + tomcat6,java日志恶梦和log4j的行为与预期不符

[英]java logging nightmare and log4j not behaving as expected with spring + tomcat6

I have a spring application that has configured log4j (via xml) and that runs on Tomcat6 that was working fine until we add a bunch of dependencies via Maven. 我有一个spring应用程序已经配置了log4j(通过xml),并且运行在Tomcat6上运行正常,直到我们通过Maven添加一堆依赖项。 At some point the whole application just started logging part of what it was supposed to be declared into the log4.xml 在某些时候,整个应用程序刚刚开始记录它应该声明的部分内容到log4.xml中

"a small rant here" Why logging has to be that hard in java world? “这里有一个小小的咆哮”为什么在java世界中日志记录必须那么难? why suddenly an application that was just fine start behaving so weird and why it's so freaking hard to debug? 为什么突然一个应用程序开始表现得如此奇怪以及为什么它如此难以调试?

I've been reading and trying to solve this issue for days but so far no luck, hopefully some expert here can give me some insights on this 我一直在阅读并试图解决这个问题好几天,但到目前为止没有运气,希望这里的一些专家可以给我一些见解

I've added log4j debug option to check whether log4j is taking reading the config file and its values and this is what part of it shows 我添加了log4j调试选项来检查log4j是否正在读取配置文件及其值,这是它的一部分显示

log4j: Level value for org.springframework.web is  [debug].
log4j: org.springframework.web level set to DEBUG
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.compass] additivity to [true].
log4j: Level value for org.compass is  [debug].
log4j: org.compass level set to DEBUG

As you can see debug is enabled for compass and spring.web but it only shows "INFO" level for both packages. 正如您所看到的,对compass和spring.web启用了调试,但它只显示了两个包的“INFO”级别。 My log4j config file has nothing out of extraordinary just a plain ConsoleAppender 我的log4j配置文件只有一个简单的ConsoleAppender

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

 <!-- Appenders -->
 <appender name="console" class="org.apache.log4j.ConsoleAppender">
  <param name="Target" value="System.out" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%-5p: %c - %m%n" />
  </layout>
 </appender>

What's the trick to make this work? 这项工作的诀窍是什么? What it's my misunderstanding here? 这是我的误解吗? Can someone point me in the right direction and explain how can I make this logging mess more bullet proof? 有人能指出我正确的方向,并解释如何使这个日志混乱更加防弹?

It might not be log4j that is doing the logging, and hence your log4j config would be ignored. 它可能不是正在执行日志记录的log4j,因此您的log4j配置将被忽略。 Spring logs using Commons Logging, which is an api that can delegate to various logging frameworks, including log4j. 使用Commons Logging的Spring日志,这是一个可以委托给各种日志框架的api,包括log4j。 To decide which implementation to use, commons logging looks into the classpath. 要确定要使用的实现,公共日志记录会查看类路径。

If you have added a dependency that dragged its own logging implementation into the classpath, commons logging might now use the other implementation. 如果添加了将其自己的日志记录实现拖到类路径中的依赖项,则commons日志记录现在可能会使用其他实现。

I recommend to set a breakpoint at a call to the logging facility, and trace the execution to see which logging implementation is used. 我建议在调用日志记录工具时设置断点,并跟踪执行情况以查看使用的日志记录实现。

As it was working until you loaded a number of dependencies via Maven, perhaps there's a Log4j configuration loaded inadvertently via these dependencies ? 由于它一直在工作,直到您通过Maven加载了许多依赖项,也许是通过这些依赖项无意中加载了Log4j配置?

Run mvn dependency:tree to see what's being loaded, and then see if any of these dependencies has a Log4j configuration. 运行mvn dependency:tree以查看正在加载的内容,然后查看是否有任何这些依赖项具有Log4j配置。

I think your problem is that you're not setting the threshold param on your appenders, and (maybe) because you're not assigning those appenders to your loggers. 我认为你的问题是你没有在你的appender上设置threshold参数,并且(也许)因为你没有将这些appender分配给你的记录器。

Try adding param name="threshold" value="debug" to your appenders and then explicitly adding them to the specific (or root) loggers like so: 尝试将param name="threshold" value="debug"到您的appender,然后将它们显式添加到特定(或根)记录器,如下所示:

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="threshold" value="debug" />
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%-5p: %c - %m%n" />
    </layout>
</appender>

<logger name="org.springframework.web">
    <level value="debug" />
    <appender-ref ref="console" />
</logger>

Also, this page says "This appender will not log any messages with priority lower than the one specified here even if the category's priority is set lower" which is probably the source of your problem. 此外, 此页面显示 “此附加程序不会记录优先级低于此处指定的消息,即使类别的优先级设置得更低”,这可能是您的问题的根源。

This seems like a good thread for you to read: http://forum.springsource.org/showthread.php?t=88250 这似乎是一个很好的线索供您阅读: http//forum.springsource.org/showthread.php?t = 88250

Cutting to the chase, it seem that the poster had a problem with a Tomcat security setting. 切入追逐,似乎海报有一个Tomcat安全设置的问题。 An updated Tomcat policy file fixed the issue. 更新的Tomcat策略文件修复了该问题。

Probably has something do with reading outside of the webapp for your log4j.xml file. 对于log4j.xml文件,可能需要在webapp外部进行读取。

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

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