在运行时更改Tomcat中的日志记录级别? [英] Change logging level in Tomcat at runtime?

查看:1268
本文介绍了在运行时更改Tomcat中的日志记录级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在当前的Tomcat 7中使用JULI日志记录框架,在 WEB-INF/classes 文件夹中提供了特定于Web应用程序的 logging.properties .

现在,我正在寻找一种在运行时更改记录器 的日志级别的方法.

我发现有几个消息来源说,可以通过 MBean Server 在运行时更改记录器的级别.不幸的是,适当的MBean "java.util.logging.Logging" 没有列出我的Webapp特定记录器,因此我无法执行方法"setLoggerLevel" .

有人知道如何做到这一点吗?感谢您提供任何提示-不包括那些建议使用log4j的提示,即...;-)

解决方案

连接 JConsole 到Tomcat并使用MBean选项卡更改记录器级别是理想的方法.一个问题是,除非代码触发了记录器的创建,否则记录器的名称不存在.您无法在代码运行之前使用MBean创建记录器.您应该使用JConsole仔细检查MBean本身是否存在.

Tomcat安装自定义LogManager 按当前类加载器进行过滤. JMX将使用系统类加载器,因此ClassLoaderLogManager 固定内存中的每个已修改记录器,以保持记录器级别处于活动状态.级别重新设置为空后,您可以释放记录器.您必须通过添加此类页面来处理安全性问题.

我不会在生产服务器上执行此操作,但是您可以配置 WatchedResource 指向您的logging.properties.然后,无论何时更新该文件,都会使用新设置重新部署Web应用程序.请注意 ClassLoader泄漏.

I am using JULI logging framework in current Tomcat 7, providing a webapp specific logging.properties in my WEB-INF/classes folder.

Now I am looking for a way to change the log level of a logger at runtime.

I found several sources saying that changing the level of a logger at runtime might be possible via MBean Server. Unfortunately the appropriate MBean "java.util.logging.Logging" does not list my webapp specific logger, so I cannot execute the method "setLoggerLevel".

Does anyone know how to accomplish this? Thanks for any hints - excluding those suggesting to use log4j, that is... ;-)

解决方案

Connecting JConsole to Tomcat and using the MBean tab to change the logger level is the ideal way. One issue is that the logger names don't exist unless the code has triggered the creation of a logger. You can't use the MBean to create a logger ahead of the code running. You should use JConsole to double check that the MBean itself doesn't exist.

Tomcat installs a custom LogManager that filters by the current class loader. JMX would be using the system class loader so it is possible that ClassLoaderLogManager won't return the logger names because the current classloader is not the web app classloader.

You could always create a servlet/jsp form or webservice deployed with the application to get the logger and set the level. You might have to pin each modified logger in memory to keep your logger level active. You can free the logger once the level is set back to null. You'll have to deal with security concerns with including such a page.

I wouldn't do this on a production server but you can configure a WatchedResource in tomcat that points to your logging.properties. Then any time that file is updated the web app is redeployed with the new settings. Watch out for ClassLoader leaks going that route.

这篇关于在运行时更改Tomcat中的日志记录级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆