1

我想将我的一个 webapp 配置为完全使用 SLF4J 而不是 Logback 进行日志记录。我想不理会 Tomcat 的其余部分,我只想确保在我的 webapp 中完成的所有日志记录(通过 Spring、Hibernate、EhCache 等)都是通过 SLF4J 通过我的 webapp 的 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 正确使用它而其他一切都没有?

4

1 回答 1

1

SLF4J 提供了用于将日志从 log4j、jul 和 commons-logging 路由到 SLF4J 的工件。有关详细信息,请参阅标题为Bridging legacy APIs的文档。

Spring 使用公共日志记录。幸运的是,SLF4J 为 commons-logging 提供了一个完美的二进制替代品,称为jcl-over-slf4j。你只需要用 jcl-over-slf4j 替换 commons-logging 依赖项。这在SLF4J FAQ中有解释。

于 2012-07-13T19:41:46.013 回答