是否可以在Tomcat中为单个Web应用程序运行Logback?

时间:2012-07-13 17:29:14

标签: java-ee tomcat slf4j logback

我想配置一个我的webapps来完全使用SLF4J over Logback进行日志记录。我想单独留下Tomcat的其余部分,我只想确保在我的webapp(Spring,Hibernate,EhCache等)中完成的所有日志记录都通过SLF4J通过我的webback的Logback上下文完成而不是通过Tomcat的默认(JUL?)日志系统。

我注意到多个库在记录时“丢失”SLF4J,这通常会在任何其他情况下使用SLF4J。 EhCache 使用SLF4J,但Spring和其他一些库正在使用JUL并忽略SLF4J:

Jul 13, 2012 11:25:45 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring'
Jul 13, 2012 11:25:45 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'spring': initialization started
Jul 13, 2012 11:25:45 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Fri Jul 13 11:25:45 PDT 2012]; root of context hierarchy
Jul 13, 2012 11:25:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
...
Jul 13, 2012 11:25:45 AM org.springframework.cache.ehcache.EhCacheManagerFactoryBean afterPropertiesSet
INFO: Initializing EHCache CacheManager
11:25:45.795 [pool-2-thread-1] DEBUG net.sf.ehcache.config.ConfigurationFactory - Configuring ehcache from InputStream
11:25:45.808 [pool-2-thread-1] DEBUG net.sf.ehcache.config.BeanHandler - Ignoring ehcache attribute xmlns:xsi
11:25:45.808 [pool-2-thread-1] DEBUG net.sf.ehcache.config.BeanHandler - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
11:25:45.809 [pool-2-thread-1] DEBUG net.sf.ehcache.config.DiskStoreConfiguration - Disk Store Path: /tmp/ehcache
11:25:45.828 [pool-2-thread-1] DEBUG net.sf.ehcache.config.ConfigurationHelper - No CacheManagerEventListenerFactory class specified. Skipping...
11:25:45.847 [pool-2-thread-1] DEBUG net.sf.ehcache.Cache - No BootstrapCacheLoaderFactory class specified. Skipping...
11:25:45.847 [pool-2-thread-1] DEBUG net.sf.ehcache.Cache - CacheWriter factory not configured. Skipping...
11:25:45.847 [pool-2-thread-1] DEBUG net.sf.ehcache.config.ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping...
11:25:45.849 [pool-2-thread-1] DEBUG net.sf.ehcache.Cache - No BootstrapCacheLoaderFactory class specified. Skipping...
11:25:45.849 [pool-2-thread-1] DEBUG net.sf.ehcache.Cache - CacheWriter factory not configured. Skipping...
11:25:45.849 [pool-2-thread-1] DEBUG net.sf.ehcache.config.ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping...
11:25:45.866 [pool-2-thread-1] DEBUG net.sf.ehcache.store.MemoryStore - Initialized net.sf.ehcache.store.NotifyingMemoryStore for encodingJobDetails.cache
11:25:45.869 [pool-2-thread-1] DEBUG net.sf.ehcache.Cache - Initialised cache: encodingJobDetails.cache
Jul 13, 2012 11:25:45 AM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/check/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.tkassembled.model.JobDetails com.tkassembled.controller.JobController.check(java.lang.String)
Jul 13, 2012 11:25:45 AM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod
INFO: Mapped "{[/submit],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.tkassembled.model.JobTicket com.tkassembled.controller.JobController.submit(com.tkassembled.model.Job)
Jul 13, 2012 11:25:46 AM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromContextPath
INFO: Creating JAXBContext with context path [com.tkassembled.model]
Jul 13, 2012 11:25:46 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'spring': initialization completed in 939 ms
Jul 13, 2012 11:25:46 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-9080"]

如何使所有支持SLF4J的库都使用Logback?为什么EhCache正确使用它而其他一切都没有?

1 个答案:

答案 0 :(得分:1)

SLF4J提供了从log4j,j.u.l路由日志的工件。和commons-logging to SLF4J。看到 标题为Bridging legacy APIs的文件,供参考。

Spring使用公共日志记录。幸运的是,SLF4J为名为jcl-over-slf4j的公共日志记录提供了完美的二进制替换。您只需要使用jcl-over-slf4j替换commons-logging依赖项。这在SLF4J FAQ

中有解释