gpt4 book ai didi

Java 日志记录说明

转载 作者:行者123 更新时间:2023-11-28 23:56:05 30 4
gpt4 key购买 nike

这是我一直没有真正完全理解的东西,不知道有没有人能解释一下?

我有一个 spring 应用程序,使用 hibernate 。我有时在 tomcat 上运行它,有时在 jetty 上运行。我从 hibernate 和 spring 获得日志输出正常。我知道在 java 日志记录方面有多种选择,例如 java.logging、slf4j、log4j 等;但根本没有真正参与配置日志记录。现在我想配置 ehcache 的日志输出我需要!

我的问题是,在我的应用程序中,我如何知道它使用的是哪种日志记录,以及如何配置和微调它?它取决于应用程序服务器吗?或者是 spring 或 hibernate 引入了自己的日志记录实现。查看我的 Maven 依赖关系树,我可以看到 hibernate-validator 引入了 org.jboss.logging,而 spring 需要 commons-logging。我也有 slf4j,因为几个包需要不同版本,所以我手动引入了最新的一个。

Maven 依赖项是:

[INFO] +- org.hibernate:hibernate-core:jar:3.6.10.Final:compile
[INFO] | +- antlr:antlr:jar:2.7.6:compile
[INFO] | +- commons-collections:commons-collections:jar:3.1:compile
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:compile
[INFO] | +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] | \- javax.transaction:jta:jar:1.1:compile
[INFO] +- org.hibernate:hibernate-c3p0:jar:3.6.10.Final:compile
[INFO] | \- c3p0:c3p0:jar:0.9.1:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.5.8:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.5.8:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.5.8:compile
[INFO] +- net.sf.ehcache:ehcache-core:jar:2.6.6:compile
[INFO] +- xerces:xercesImpl:jar:2.10.0:compile
[INFO] | \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] +- com.vividsolutions:jts:jar:1.13:compile
[INFO] +- org.hibernatespatial:hibernate-spatial-postgis:jar:1.1:compile
[INFO] | \- org.hibernatespatial:hibernate-spatial:jar:1.1:compile
[INFO] +- postgresql:postgresql:jar:9.1-901-1.jdbc4:compile
[INFO] +- org.postgis:postgis-jdbc:jar:1.3.3:compile
[INFO] | \- org.postgis:postgis-stubs:jar:1.3.3:compile
[INFO] +- org.springframework:spring-expression:jar:3.1.2.RELEASE:compile
[INFO] | \- org.springframework:spring-core:jar:3.1.2.RELEASE:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- org.springframework:spring-beans:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:3.1.2.RELEASE:compile
[INFO] | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] +- org.springframework:spring-context:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-tx:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-oxm:jar:3.1.2.RELEASE:compile
[INFO] | \- commons-lang:commons-lang:jar:2.5:compile
[INFO] +- org.springframework:spring-web:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-aspects:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-instrument:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-instrument-tomcat:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-jms:jar:3.1.2.RELEASE:compile
[INFO] +- org.springframework:spring-asm:jar:3.1.4.RELEASE:compile
[INFO] +- org.aspectj:aspectjrt:jar:1.7.1:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.7.1:compile
[INFO] +- cglib:cglib:jar:2.2.2:compile
[INFO] | \- asm:asm:jar:3.3.1:compile
[INFO] +- org.springframework.security:spring-security-core:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-web:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-config:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-aspects:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-taglibs:jar:3.1.3.RELEASE:compile
[INFO] | \- org.springframework.security:spring-security-acl:jar:3.1.3.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-crypto:jar:3.1.3.RELEASE:compile
[INFO] +- org.hibernate:hibernate-validator:jar:4.3.1.Final:compile
[INFO] | +- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] | \- org.jboss.logging:jboss-logging:jar:3.1.0.CR2:compile
[INFO] +- org.hibernate:hibernate-validator-annotation-processor:jar:4.3.1.Final:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.1.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.1.1:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.1.1:compile
[INFO] +- org.apache.velocity:velocity:jar:1.7:compile
[INFO] +- joda-time:joda-time:jar:2.1:compile
[INFO] +- javax.mail:mail:jar:1.4.5:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] +- jstl:jstl:jar:1.2:compile
[INFO] +- javaxt:javaxt-core:jar:1.5.10:compile
[INFO] +- commons-fileupload:commons-fileupload:jar:1.2.2:compile
[INFO] +- commons-io:commons-io:jar:2.4:compile
[INFO] +- org.springframework:spring-test:jar:3.1.2.RELEASE:test
[INFO] +- junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.mockito:mockito-all:jar:1.9.5:test
[INFO] +- org.glassfish:javax.servlet:jar:3.0:test
[INFO] +- javax:javaee-web-api:jar:6.0:provided
[INFO] \- javassist:javassist:jar:3.12.1.GA:compile

干杯,自然灾害

最佳答案

我的问题是,在我的应用程序中,我如何知道它使用的是哪种日志记录,以及如何配置和微调它?

首先你应该注意到commons logging不是日志框架,它只是不同日志框架之间的桥梁/门面。因此,如果一个框架使用公共(public)日志记录,那么您可以“配置”公共(public)日志记录以将日志记录语句转发到真正的日志记录框架(log4j、slf4j、java 日志记录...)

其次,您应该知道,如果一个框架(spring、hibernate 等)使用 loggingframework X,那么您就不会在意它。您需要提供该日志记录框架。但有一个窍门。

Slf4j 提供了一个“替换”其他框架的技巧,假设:

  • 你有框架 X,例如使用 Log4j
  • 但是您没有将 log4j 与您的应用程序一起发布,而是另一个实现桥接模式的库(它具有与 Log4j 相同的接口(interface)(接口(interface)和类),但在内部它将日志语句转发到 slf4j。(例如:log4j -over-slf4j)

通过这个技巧,您可以将来自不同框架的所有日志语句统一到一个日志框架中,这些框架被实现为使用不同的日志框架。

顺便说一句:你应该知道 Slf4j 本身只是一个 Facade/Bridge,就像公共(public)日志记录一样:- 您可以将日志记录语句转发到您想要的特殊日志记录框架- 或 native 实现 logback

我强烈建议阅读 slf4j manualBridging legacy APIs

是否取决于应用服务器?我只能代表 Tomcat(您提到过):默认情况下,Tomcat 不为您的应用程序提供日志记录框架。

顺便说一句:Tomcat 是一个 Servlet 容器,没有应用程序服务器。


根据 Ehcache Documentation (当谷歌搜索“Ehcache logging”时首次命中),它使用 slf4j。

关于Java 日志记录说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17934873/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com