我正在尝试使用SLF4JBridgeHandler和jul-to-slf4j,以便可以使用Logback Logstash编码器将Tomcat日志转换为JSON格式。我们已经在部署应用程序以使用该编码器将我们的应用程序日志转换为JSON格式,现在我们希望对容器日志,访问日志等执行相同的操作...我们正在尝试对Tomee执行此操作7.0.1部署(Tomee 7.1部署Tomcat 8.5.3)。
为了进行配置,我将这些罐子复制到以下位置:
$CATALINA_HOME/bin/jul-to-slf4j-1.7.25.jar
$CATALINA_HOME/bin/slf4j-api-1.7.25.jar
$CATALINA_HOME/bin/logback-classic-1.2.3.jar
$CATALINA_HOME/bin/logback-core-1.2.3.jar
$CATALINA_HOME/bin/logstash-logback-encoder-5.2.jar
我创建一个$ CATALINA_HOME / bin / setenv.sh来设置CLASSPATH以包含新的jar和logback.xml配置所在的目录
CLASSPATH=$CATALINA_HOME/bin/jul-to-slf4j-1.7.25.jar:$CATALINA_HOME/bin/slf4j-api-1.7.25.jar:$CATALINA_HOME/bin/logback-classic-1.2.3.jar:$CATALINA_HOME/bin/logback-core-1.2.3.jar:$CATALINA_HOME/bin/logstash-logback-encoder-5.2.jar:$CATALINA_HOME/bin/logback-config
我已经用一行代码将logging.properties文件复制到$ CATALINA_HOME / conf / logging.properties中
handlers = org.slf4j.bridge.SLF4JBridgeHandler
...最后,我创建了一个简单的logback.xml
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/usr/local/tomcat/logs/my.log</file>
<encoder>
<Pattern>%d{HH:mm} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
当我尝试启动Tomcat时,它失败并显示此错误
19:47:12.548 [localhost-startStop-1] ERROR org.apache.catalina.core.ContainerBase - ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/docs]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:158)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1107)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1841)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContainerInitializer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1268)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1116)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:188)
at org.apache.catalina.startup.WebappServiceLoader.load(WebappServiceLoader.java:159)
at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1611)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1131)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:298)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5076)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152)
... 10 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContainerInitializer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 38 common frames omitted
我的理解是,错误与类路径上某个地方的旧servlet-api版本有关。如果我将logback版本降级到1.1.9(而不是1.2.3),那么我将不再收到任何错误,并且Tomcat可以正常启动...但是,我的logback.xml似乎没有得到认可,因为我没有看到我期望的日志文件正在创建。显然,也没有创建典型的catalina.log等...配置了某些内容,而并非我期望的那样。
有人可以看到我在做什么吗?是否有比我正在尝试的方法更好的方法?
谢谢!