tomcat 中的自定义 java.util.logging 处理程序 [英] Custom java.util.logging Handler in tomcat

查看:26
本文介绍了tomcat 中的自定义 java.util.logging 处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在给定系统上的所有 webapp 之间有一些通用的日志配置,我们试图将这些配置外化到 tomcat 级别,而不是尝试在单个 webapp 级别进行处理.事实证明,使用 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,Java 6.默认的 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.

首先,大致遵循此答案中的建议,我创建了自定义处理程序并将 jar 放入 $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

然后我修改了 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

然而,当我运行 ./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).

如果我将 jar 直接添加到 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 引导类加载器中.因此,必须将带有自定义处理程序和所需依赖项的 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屋!

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