jakarta-ee - 是否可以在 Tomcat 中为单个 webapp 运行 Logback?

标签 jakarta-ee tomcat slf4j logback

我想将我的一个 Web 应用程序配置为完全使用 SLF4J over 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 正确使用它而其他一切都不是?

最佳答案

SLF4J 提供了用于从 log4j、j.u.l. 路由日志的工件。和公共(public)日志记录到 SLF4J。看 题为Bridging legacy APIs的文件了解详情。

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

关于jakarta-ee - 是否可以在 Tomcat 中为单个 webapp 运行 Logback?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11475432/

相关文章:

java - 在 Safari 中, session 不会在后续的 POST 和 GET 之间保持

oracle - 在 JDBC 连接中使用 TNS 名称和服务名称的区别

java - SLF4J 和记录器工厂

java - 使用 ServletContextListener 和 SLF4J 在 contextDestroyed 上没有日志输出

java - SLF4J和Logback : Propagate logger to autowired services

jakarta-ee - 使用部署描述符的 EJB 注入(inject)

java - JSF 2、EL Expression、Weblogic 和 EJB 注入(inject)

java - CDI 注入(inject)未在 Jackson 序列化程序中解决

java - 如何使用与 tomcat 不同的用户在 tomcat 中运行我的 Web 应用程序?

configuration - 在 EAR 文件中保留配置设置的最佳实践是什么?