java 日志记录噩梦和 log4j 在 spring 中的行为不符合预期汤姆猫6
我有一个 Spring 应用程序,它配置了 log4j (通过 xml),并且在 Tomcat6 上运行,该应用程序工作正常,直到我们通过 Maven 添加一堆依赖项。在某些时候,整个应用程序刚刚开始记录其应该声明到 log4.xml 中的部分内容
“这里有一个小咆哮”为什么在 Java 世界中记录必须那么困难?为什么突然一个本来很好的应用程序开始表现得如此奇怪,并且为什么它如此难以调试?
我已经阅读并尝试解决这个问题好几天了,但到目前为止还没有运气,希望这里的一些专家能给我一些关于这个问题的见解
我添加了 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
正如您所看到的,compass 和 spring.web 启用了调试,但它只显示这两个包的“INFO”级别。我的 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>
使这项工作有效的技巧是什么?我这里有什么误会吗? 有人能给我指出正确的方向并解释如何让这个日志混乱更加防弹吗?
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. At some point the whole application just started logging part of what it was supposed to be declared into the log4.xml
"a small rant here" Why logging has to be that hard in java world? 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: 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. My log4j config file has nothing out of extraordinary just a plain 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能不是 log4j 正在执行日志记录,因此您的 log4j 配置将被忽略。 Spring 使用 Commons Logging 记录日志,这是一个可以委托给各种日志框架(包括 log4j)的 api。为了决定使用哪个实现,公共日志记录会查看类路径。
如果您添加了一个依赖项,将其自己的日志记录实现拖到类路径中,则公共日志记录现在可能会使用其他实现。
我建议在调用日志记录工具时设置断点,并跟踪执行情况以查看使用了哪个日志记录实现。
It might not be log4j that is doing the logging, and hence your log4j config would be ignored. Spring logs using Commons Logging, which is an api that can delegate to various logging frameworks, including 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.
I recommend to set a breakpoint at a call to the logging facility, and trace the execution to see which logging implementation is used.
因为它一直在工作,直到您通过 Maven 加载了许多依赖项,所以也许有一个 Log4j 配置无意中通过这些依赖项加载了?
运行
mvn dependency:tree
以查看正在加载的内容,然后查看这些依赖项中的任何一个是否具有 Log4j 配置。As it was working until you loaded a number of dependencies via Maven, perhaps there's a Log4j configuration loaded inadvertently via these dependencies ?
Run
mvn dependency:tree
to see what's being loaded, and then see if any of these dependencies has a Log4j configuration.我认为您的问题是您没有在附加程序上设置
阈值
参数,并且(可能)因为您没有将这些附加程序分配给您的记录器。尝试将
param name="threshold" value="debug"
添加到您的附加程序,然后将它们显式添加到特定(或根)记录器,如下所示:此外, 此页面 表示“即使类别的优先级设置较低,此附加程序也不会记录优先级低于此处指定的任何消息“这可能是你的问题的根源。
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.Try adding
param name="threshold" value="debug"
to your appenders and then explicitly adding them to the specific (or root) loggers like so: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.
这似乎是一个适合您阅读的主题:
http://forum.springsource.org/showthread.php?t=88250
切入正题,发帖者的 Tomcat 安全设置似乎存在问题。更新的 Tomcat 策略文件修复了该问题。
可能与在 web 应用程序外部读取 log4j.xml 文件有关。
This seems like a good thread for you to read:
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. An updated Tomcat policy file fixed the issue.
Probably has something do with reading outside of the webapp for your log4j.xml file.