java - 如何在 spring 中禁用调试日志

标签 java spring

如何在我的 Spring 应用程序中禁用 Debug模式。当我在生产环境中运行应用程序时,它会提供大量调试日志语句,并且 tomcat 的日志文件会占用更多磁盘空间。就像下面这样,

05:03:26.340 [http-bio-1880-exec-2] DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
05:03:26.340 [http-bio-1880-exec-2] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.s.w.u.m.AntPathRequestMatcher - Checking match of request : '/myRestCall/1234'; against '/'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 1 of 8 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No HttpSession currently exists
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: null. A new one will be created.
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 2 of 8 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 3 of 8 in additional filter chain; firing Filter: 'CustomUserNamePasswordAuthenticationFilter'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 4 of 8 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 5 of 8 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 6 of 8 in additional filter chain; firing Filter: 'SessionManagementFilter'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 7 of 8 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 at position 8 of 8 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Public object - authentication not attempted
05:03:30.118 [http-bio-1880-exec-2] DEBUG o.s.security.web.FilterChainProxy - /myRestCall/1234 reached end of additional filter chain; proceeding with original 

这是我的依赖

            <!-- logging into console -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.0.13</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.7.5</version>           
    </dependency>

如何禁用所有调试日志,我只想显示警告和异常日志。

根据结果,我创建了 logback.xml(src/main/resources/config/logback.xml) 文件,如下所示,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
<logger name="org.springframework" level="ERROR"/>
  <root level="ERROR">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

但仍然在控制台中调试日志打印。我哪里做错了?

最佳答案

在某个地方(通常在类路径中),您有一个 logback.xml configuration此应用程序的文件,编辑它并添加

<logger name="org.springframework" level="WARNING"/>

之前

<root level="XXX">

另请注意,您可能应该 "rotate" your log files以减少磁盘使用。

关于java - 如何在 spring 中禁用调试日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37299755/

相关文章:

Java 卡转换

java - 覆盖默认的 org.springframework.amqp.rabbit.connection.CachingConnectionFactory 实现

spring - ActiveMQ 未释放队列的磁盘存储空间

javax.el.PropertyNotFoundException : Property 'id' not found on type model. 杂货店

java - SSH 连接到远程 Linux 服务器但不使用第 3 方库

java - 项目可以检测导入语句,但不会使用它

Spring计划任务数据源

java - 如果值为 null,则保留 FreeMarker 标记

java - 使用 Spring 访问 XmlBeanFactory 的更好设计

java - @Value 不在扩展其他类的类中工作