如何禁用tomcat 7控制台调试信息

Posted

技术标签:

【中文标题】如何禁用tomcat 7控制台调试信息【英文标题】:how to disable tomcat 7 console debug information 【发布时间】:2013-11-28 11:26:30 【问题描述】:

其实这个问题我已经问了很久了,我用的是SpringMVC + Hibernate3 + Spring Security作为框架,tomcat 7作为容器,但是在web.xml中添加过滤器和拦截器后,控制台打印了很多调试信息,无论我如何更改配置文件,这些调试信息总是在那里,请帮助。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>iCubeHRS</display-name>

   <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>           
            /WEB-INF/dispatcher-servlet.xml
            /WEB-INF/spring-security.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

   <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

log4j

# Root logger option
log4j.rootLogger=WARN, file
log4j.category.org.springframework = OFF

log4j.logger.org.springframework.aop.framework.autoproxy=OFF

log4j.logger.org.apache=OFF,R

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=c\:\\test\\logging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%dyyyy-MM-dd HH:mm:ss %-5p %c1:%L - %m%n

控制台中那些血腥的调试信息(比我贴在这里的要多得多):

14:16:21.617 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
14:16:21.618 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
14:16:21.618 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@79d4461c]
14:16:21.619 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
14:16:21.625 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
14:16:21.626 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
14:16:21.626 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@63f9e51e]
14:16:21.627 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0'
14:16:21.627 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping'
14:16:21.627 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1'
14:16:21.627 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0'
14:16:21.627 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#1'
14:16:21.628 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0'
14:16:21.628 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping'
14:16:21.629 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1'
14:16:21.629 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0'
14:16:21.629 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#1'
14:16:21.629 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter'
14:16:21.629 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter'
14:16:21.630 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0'
14:16:21.630 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'
14:16:21.631 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter'
14:16:21.631 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter'
14:16:21.631 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0'
14:16:21.631 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'
14:16:21.632 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0'
14:16:21.632 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0'
14:16:21.632 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0'
14:16:21.633 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0'
14:16:21.633 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0'
14:16:21.633 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0'
14:16:21.635 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
14:16:21.642 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
14:16:21.643 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
14:16:21.643 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@2111dbcb]
14:16:21.644 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.view.InternalResourceViewResolver#0'
14:16:21.645 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.web.servlet.view.InternalResourceViewResolver#0'
14:16:21.648 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
14:16:21.655 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
14:16:21.656 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
14:16:21.656 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@22f73351]
14:16:21.659 [localhost-startStop-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalScheduledAnnotationProcessor'
14:16:21.659 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet 'dispatcher' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher]
14:16:21.659 [localhost-startStop-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 2535 ms
14:16:21.659 [localhost-startStop-1] DEBUG o.s.web.servlet.DispatcherServlet - Servlet 'dispatcher' configured successfully

======================================更新1 ======== ===============================

更新的 Log4j 文件

log4j.rootLogger=WARN,console,R

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=WARN
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.conversionPattern=%dyyyy-MM-dd HH:mm:ss [%5p] - %c -%F(%L) -%m%n

log4j.org.springframework.web=WARN

log4j.logger.org.springframework.web.servlet.mvc = OFF

log4j.logger.org.springframework.security=OFF 

org.springframework.security.level=WARN

【问题讨论】:

你知道你的配置有缺陷(你基本上是在复制你的 bean!)。 ContextLoaderListenerDispatcherServlet 都在加载 dispatcher-servlet.xml。还要确保您在其他地方没有 log4j(例如在 tomcat 内部),因为它无法读取您的 log4j 配置。还要确保您没有重复的配置(log4j.xml 和 log4j.properties,我强烈建议使用第一个选项,因为恕我直言,它更易于阅读)。 @M.Deinum 我将 dispather-servlet.xml 放入 ContextConfiguration 范围的原因是我必须在 spring-security 之前初始化所有数据库连接.xml 已加载,因为我需要将 datasource bean 注入 spring-security.xml 以实现身份验证模块,正如您所提到的,这将导致“复制您的 bean”问题,但我没有收到任何错误或异常,如果它确实重复,如何避免它,谢谢 @M.Deinum 如果我从 ContextConfiguration 范围中删除 dispatcher-servlet.xml,是否有助于避免重复 bean 问题? 是的。它不会给你错误,但它(可能并且在某些时候会)导致奇怪的事务问题,可能是内存问题等。所以应该避免重复。一般来说,您的 ContextLoaderListener 应该加载所有通用的东西(数据源、服务、存储库等),而您的 DispatcherServlet 只加载与 Web 相关的资源(控制器、视图解析器、视图等)。 如前所述,如果您的 Web 应用程序(或应用服务器路径中的某处)中没有 log4j,则配置基本上是无用的。所以我的猜测是您的应用程序服务器中有 log4j(并且在应用程序中加载之前)。这意味着无法从 Web 应用程序内部加载资源。 【参考方案1】:

其实答案就在这里https://***.com/a/23254829/1000011

在资源目录中创建一个 logback.xml 文件,其中包含:

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>web - %date [%thread] %-5level %logger36 - %message%n
            </pattern>
        </encoder>
    </appender>

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

    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

【讨论】:

【参考方案2】:

(在 cmets 中回答的问题和 OP 编辑​​的问题。见 Question with no answers, but issue solved in the comments (or extended in chat))

OP 写道:

要禁用控制台调试信息,你需要添加log4j配置文件,然后将日志级别修改为DEBUG以上,在这种情况下,我的log4j不能正常工作'因为我放了另一个日志库进入项目,正如@M.Deinum 所提到的,'确保您在其他地方没有 log4j 或其他日志库'

web.xml 中存在缺陷(基本上是复制bean),因为我将dispatcher-servlet.xml 放入上下文加载器配置范围,之后调度程序servlet 再次加载它,有时我们需要先初始化一些bean,然后再初始化一些bean,比如数据源bean ,在我的情况下,spring security 需要一个数据源在请求进入调度程序之前读取权限,所以我必须首先初始化数据源,就像@M.Deinum 提到的那样,“通常你的 ContextLoaderListener 应该加载所有通用的东西(数据源、服务、存储库等),而您的 DispatcherServlet 仅加载与 Web 相关的资源(控制器、视图解析器、视图等)。”解决了这个问题。

【讨论】:

以上是关于如何禁用tomcat 7控制台调试信息的主要内容,如果未能解决你的问题,请参考以下文章

如何在 React-Native 中禁用远程 JS 调试

怎样查看Tomcat动态控制台信息

如何调试 DJango 测试服务器提供的错误 500?

Linux实时查看Tomcat调试信息

如何控制 glib 中调试信息的级别?

linux kernel 没有输出信息 怎么调试