无法禁用日志消息

2024-01-10

我在摆脱 Spring 生成的调试消息时遇到了一些麻烦(类似于以下消息;有数千个这样的条目):

19:58:08.380 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'propertyPlaceholderConfigurer'
19:58:08.380 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'propertyPlaceholderConfigurer'
19:58:08.383 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'appConfig'
19:58:08.383 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'appConfig'
19:58:08.383 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'appConfig' to allow for resolving potential circular references
19:58:08.384 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'appConfig'

在相关问题中,有很多建议涉及log4j,web.xml,......
但是,我没有使用其中任何一个 - 我只是实例化一个AnnotationConfigApplicationContext并开始创建bean。

在我的 pom.xml 文件中,没有对任何日志记录框架的引用 - 我只包含 spring 依赖项:

<!-- Spring and Transactions -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-framework.version}</version>
</dependency>
<!-- ... --> 
    <artifactId>spring-tx</artifactId>
    <!-- ... --> 
    <artifactId>spring-boot-starter</artifactId>
    <!-- ... --> 
    <artifactId>spring-web</artifactId>
<!-- ... --> 

我在某处读到 Spring 似乎默认使用“Commons 日志记录”,但我尝试禁用它但没有成功(如关闭 Apache 通用日志记录 https://stackoverflow.com/questions/1436761/turn-off-apache-common-logging ):

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

此外,我尝试通过添加以下内容来排除 pom.xml 中的公共日志记录:

<exclusions>
   <!-- Exclude Commons Logging in favor of SLF4j -->
   <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
   </exclusion>
</exclusions>

然而,仍然没有运气。

接下来,我尝试包含对 log4j 的依赖项,希望这会覆盖默认日志记录。由于消息的格式保持不变,看来这次尝试也没有成功。

接下来我可以尝试什么?


第一:slf4j 是否在类路径上?

SLF4J是Java的另一种日志抽象,也可以与Spring框架一起使用。许多库/产品已切换到 slf4j。

名称中是否有任何包含“slf4j”的依赖项?尝试 mvn dependency:tree -Dverbose=true,然后查看是否出现 slf4j。如果是这样,请访问 slf4j 网站以获取有关其设置的更多信息。

第二:使用哪个log4j配置文件?

检测是否使用 log4j 以及 log4j 配置文件是否位于类路径中的提示: 尝试将属性 log4j.debug 设置为“true”。

使用 mvn exec:java 时,只需在命令行中添加 -Dlog4j.debug=true 即可。

如果这是使用 maven Surefire 插件的 Junit 测试,请尝试在 Surefire 插件本身中设置 systemProperties:http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法禁用日志消息 的相关文章

随机推荐