log4j 日志打印2遍

围城丶 发布于 2016/07/26 19:34
阅读 1K+
收藏 1

日志如下:

[INFO] org.springframework.web.context.ContextLoader,2016-07-26 19:22:29,865,Root WebApplicationContext: initialization started
[INFO] org.springframework.web.context.ContextLoader,2016-07-26 19:22:29,865,Root WebApplicationContext: initialization started
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,141,Initializing new StandardServletEnvironment
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,141,Initializing new StandardServletEnvironment
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,144,Adding [servletConfigInitParams] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,144,Adding [servletConfigInitParams] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,145,Adding [servletContextInitParams] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,145,Adding [servletContextInitParams] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,160,Adding [jndiProperties] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,160,Adding [jndiProperties] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,161,Adding [systemProperties] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,161,Adding [systemProperties] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,161,Adding [systemEnvironment] PropertySource with lowest search precedence
[DEBUG] org.springframework.web.context.support.StandardServletEnvironment,2016-07-26 19:22:30,161,Adding [systemEnvironment] PropertySource with lowest search precedence

全部输出2次,感觉像是项目启动了2次一样.

log4j.xml配置如下:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">


<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">


  <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="[%p] %c,%d{yyyy-MM-dd HH🇲🇲ss,SSS},%m%n"/>
    </layout>
  </appender>


  <appender name="DEBUG_FILE_LOG" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="true"/>
    <param name="encoding" value="UTF-8"/>
    <param name="file" value="F:/Log/javawc-web-debug.log"/>
    <param name="MaxFileSize" value="100MB"/>
    <param name="MaxBackupIndex" value="10000"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="[%p] %c,%d{yyyy-MM-dd HH🇲🇲ss,SSS},%m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="DEBUG"/>
    </filter>
  </appender>


  <appender name="ERROR_FILE_LOG" class="org.apache.log4j.RollingFileAppender">
    <param name="append" value="true"/>
    <param name="encoding" value="UTF-8"/>
    <param name="file" value="F:/Log/javawc-web-error.log"/>
    <param name="MaxFileSize" value="100MB"/>
    <param name="MaxBackupIndex" value="1000"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern"
             value="[%p] %c,%d{yyyy-MM-dd HH🇲🇲ss,SSS},%m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="DEBUG"/>
    </filter>
  </appender>


  <logger name="java.sql" additivity="true">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>


  <logger name="java.sql.Statement" additivity="true">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>
  <logger name="java.sql.PreparedStatement" additivity="true">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>
  <logger name="java.sql.ResultSet" additivity="true">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>
  <logger name="java.sql.Connection" additivity="true">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>


  <logger name="java.lang.SecurityException">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>


  <logger name="org.springframework" additivity="true">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>


  <logger name="org.apache.ibatis" additivity="true">
    <level value="DEBUG"/>
    <appender-ref ref="CONSOLE"/>
  </logger>


  <!--OFF-->
  <category name="org.apache" additivity="true">
    <priority value="ON"/>
  </category>


  <category name="com.javawc.service" additivity="true">
    <priority value="DEBUG"/>
  </category>


  <root>
    <priority value="DEBUG"/>
    <appender-ref ref="ERROR_FILE_LOG"/>
    <appender-ref ref="DEBUG_FILE_LOG"/>
    <appender-ref ref="CONSOLE"/>
  </root>
</log4j:configuration>
因为项目启动之后报错了, 异常如下:

Caused by: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 73; 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"。

我查找资料说因为重复加载了某个XML导致的,所以大神帮我定位一下,上面打印日志重复是log4j的配置问题导致打印了2遍,还是项目有别的问题导致启动了2次呢.

加载中
0
小王爷威武

因为配置了这一段:


<logger name="org.springframework" additivity="true"> <level value="DEBUG"/> <appender-ref ref="CONSOLE"/> </logger>
所以org.springframework.web.context.ContextLoader就打印了日志。


而且还配置了additivity="true",所以还会找上一级日志:org.springframework.web.context,

没有的话,再找org.springframework.web,依此类推,找到org都没有,再往上找就是root了。

<root> <priority value="DEBUG"/> <appender-ref ref="ERROR_FILE_LOG"/> <appender-ref ref="DEBUG_FILE_LOG"/> <appender-ref ref="CONSOLE"/> </root>
root你已经配置了,所以又打印了


0
围城丶

引用来自“小王爷威武”的评论

因为配置了这一段:


<logger name="org.springframework" additivity="true"> <level value="DEBUG"/> <appender-ref ref="CONSOLE"/> </logger>
所以org.springframework.web.context.ContextLoader就打印了日志。


而且还配置了additivity="true",所以还会找上一级日志:org.springframework.web.context,

没有的话,再找org.springframework.web,依此类推,找到org都没有,再往上找就是root了。

<root> <priority value="DEBUG"/> <appender-ref ref="ERROR_FILE_LOG"/> <appender-ref ref="DEBUG_FILE_LOG"/> <appender-ref ref="CONSOLE"/> </root>
root你已经配置了,所以又打印了


谢谢您,您的答案说的很对,我已经解决了问题.
返回顶部
顶部