tomcat中的自定义java.util.logging处理程序 [英] Custom java.util.logging Handler in tomcat
问题描述
在给定系统上,所有Web应用程序之间都有一些通用的日志记录配置,我们试图将其外部化为tomcat级别,而不是尝试在单个Web应用程序级别上进行处理.事实证明,使用java.util.logging
的Web应用程序具有一定的挑战性,因为我们有一个自定义处理程序,而且似乎没有一种明显的方法可以使该自定义处理程序与tomcat的类加载器很好地配合使用.目前,这一切都在原型阶段.
We have some common logging configuration between all of the webapps on a given system that we are trying to externalize to the level of tomcat, rather than trying to handle at the level of the individual webapp. The webapps that are using java.util.logging
are proving to be somewhat challenging because we have a custom handler, and there doesn't seem to be an obvious way to get the custom handler to play nicely with tomcat's classloaders. This is all in the prototype stage at the moment.
初步:Tomcat 7.0.32,Java6.默认安装的tomcat 7安装了一个REST服务,并且配置中没有任何问题.
Preliminary: Tomcat 7.0.32, Java 6. Default tomcat 7 install with one REST service deployed and nothing funny in the configuration.
首先,大致按照此答案中的建议,我创建了自定义处理程序并将罐子放入$CATALINA_HOME/lib
并确认该目录在正确的目录中,并且common.loader
包含此目录:
First, roughly following the advice in this answer, I created the custom handler and put the jar in $CATALINA_HOME/lib
and confirmed that said directory was in the right directory and that the common.loader
included this directory:
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
然后我修改了logging.properties
文件并添加了处理程序:
Then I modified the logging.properties
file and added the handler:
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler, my.custom.Handler
handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler, my.custom.Handler
但是,当我运行./startup.sh
时,会得到以下信息:
When I run ./startup.sh
, however, I get the following:
[Loaded java.io.PrintWriter from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.util.logging.StreamHandler from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
[Loaded java.util.logging.ConsoleHandler from /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar]
Handler error
java.lang.ClassNotFoundException: my.custom.Handler
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at org.apache.juli.ClassLoaderLogManager.readConfiguration(ClassLoaderLogManager.java:521)
at org.apache.juli.ClassLoaderLogManager.readConfiguration(ClassLoaderLogManager.java:464)
at org.apache.juli.ClassLoaderLogManager.readConfiguration(ClassLoaderLogManager.java:288)
at java.util.logging.LogManager$2.run(LogManager.java:267) [...]
(与JAVA_OPTS=-verbose:class
一起使用).
我已经看到稍后加载了相关的类,但这似乎并不一致,并且可能是上述REST服务(直接使用它)的产物.
I have seen the relevant classes loaded later, but this does not appear to be consistent and is probably an artifact of the aforementioned REST service (which was using it directly).
如果直接将罐子添加到CLASSPATH
中,我可以使一切正常工作,但是与修改装载程序相反,通常不鼓励这样做.
I can get everything to work correctly if I add the jar to the CLASSPATH
directly, but this seems to be generally discouraged as opposed to modifying the loaders.
在读取logging.properties
之前如何干净地将自定义java.util.logging.Handler
(以及后来的格式化程序)添加到类加载器时,我是否缺少某些特殊之处?
Is there something particular that I am missing in how to cleanly add a custom java.util.logging.Handler
(and, later, a formatter) to the classloader before logging.properties
is read?
或者,如果我一团糟地错误树,我会采用更好的方法来解决多个Web应用程序之间使用java.util.logging
的共享日志记录配置的问题.
Alternatively, if I'm flat barking up the wrong tree, I'd take a better approach to the problem of a shared logging configuration between multiple webapps with java.util.logging
.
推荐答案
对于Tomcat范围的自定义日志记录,您需要将类注入Tomcat Bootstrap ClassLoader.因此,必须将具有自定义处理程序和所需依赖项的jar放入启动脚本CLASSPATH中.我建议在$ CATALINA_BASE/bin/setenv.sh上添加一个自定义脚本,即
For the Tomcat-wide custom logging you need to inject your class into the Tomcat bootstrap ClassLoader. Thus jar with custom Handler and required dependencies have to be put into the startup script CLASSPATH. I'd advice to a add custom script at $CATALINA_BASE/bin/setenv.sh, i.e.
#!/bin/sh
CLASSPATH="$CATALINA_BASE/bin/myhandler.jar"
或者您可以在Tomcat启动期间加载脚本变量时动态收集所需的jar.
or you can collect required jars dynamically as script variables are loaded during the Tomcat start-up.
这篇关于tomcat中的自定义java.util.logging处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!