在运行时重新加载 tomcat 日志记录? [英] Reload tomcat logging at runtime?

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

问题描述

我们目前使用 Tomcat 7 和使用 -Djava.util.logging.config.file 指定的单个日志配置和使用 -Djava.util.logging.manager="org.apache.juli.ClassLoaderLogManager" 的默认 ClassLoaderLogManager.这对于一次性启动配置非常有用.

We are currently using Tomcat 7 with a single log configuration specified using -Djava.util.logging.config.file and the default ClassLoaderLogManager with -Djava.util.logging.manager="org.apache.juli.ClassLoaderLogManager". This works great for a one-time startup config.

我们有几个 servlet 和其他在 servlet 上下文之外运行的代码.我们在完全控制的专用 tomcat 服务器上运行,我们希望所有代码都使用相同的日志配置.我们使用 java.util.logging API 进行日志记录.这意味着 LogManager.getLogManager().getLogger(name) 需要工作,Logger.isLoggable(Level) 需要工作.

We have several servlets and other code that runs outside the servlet context. We run on a dedicated tomcat server that we completely control, and we want all the code to use the same log configuration. We are using the java.util.logging API for logging. This means that LogManager.getLogManager().getLogger(name) needs to work, and that Logger.isLoggable(Level) needs to work.

ClassLoaderLogManager 似乎与我们的情况相反:允许 servlet 指定单独的日志设置.我们希望在一个地方控制所有日志.但是,我们确实希望获得 JULI 的其他好处,例如改进的 FileHandler.

ClassLoaderLogManager seems geared towards the opposite of our situation: allowing servlets to specify individual log settings. We want all the logs controlled in one place. But, we do want the other JULI benefits like the improved FileHandlers.

现在的问题是:如何在不重新加载应用程序的情况下在运行时重新加载文件中的这些设置?

Now the question: How can I reload these settings from the file at runtime without reloading the application?

我尝试过的:

  • LogManager.getLogManger.readConfiguration():在 ClassLoaderLogManager 中产生有效的 NOOP,因为 Thread.currentThread.getContextClassLoader() 不是系统类加载器.
  • 显式设置 Thread.setContextClassLoader(ClassLoader.getSystemClassLoader()) 然后调用上面的.这确实读取了配置文件(在调试器中逐步执行),但它没有将更改向下传播到包含的类加载器中的现有记录器.从未在现有记录器上调用 Logger.setLevel().
  • 在这些调用之前也调用 reset() 似乎没有任何改变.
  • JMX 似乎只公开单个 ClassLoader(可能是系统 ClassLoader)的记录器

推荐答案

我找到了一个解决方案.将启动脚本中的日志管理器替换为默认的 java.util.logging.LogManager 或简单地删除命令行参数会导致使用常规 LogManager.当 readConfiguration() 被调用时,这个 LogManager 将完全重新加载所有类加载器中所有记录器的配置,这正是我需要的行为.

I found one solution to this. Replacing the log manager in the startup script with the default java.util.logging.LogManager or simply deleting the command line argument results in the regular LogManager being used. This LogManager will fully reload the configuration for all Loggers in all ClassLoaders when readConfiguration() is called, exactly the behavior I need.

不过,这确实涉及修改 tomcat 启动脚本.如果有人可以在不这样做的情况下找到更好的解决方案,那就太好了,否则我会接受这个答案.

This does involve modifying the tomcat startup scripts, however. If someone can find a better solution without doing that, that would be great, otherwise I'll accept this answer.

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

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