没有为log4j2添加RollingFile Appenders

时间:2019-07-15 07:52:10

标签: xml log4j2

我正在为我的Web应用程序设置log4j2。问题是这样配置的记录器没有使用Main和Err附加程序记录任何内容。如果人们通过调试器查看log4j2代码,就会看到在log4j2初始化期间未添加这两个附加程序。

以前,每个附加程序都有单独的记录器,但是我们只需要一个记录器。可以这样做吗?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" strict="true" name="log4j2 config">
  <Properties>
    <Property name="dataPattern">%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{3} - %m%n%ex</Property>
    <Property name="logDir">${web:rootDir}</Property>
    <Property name="moduleName">mod</Property>
    <Property name="logLevel">@log.level@</Property>
  </Properties>

  <Filter type="ThresholdFilter" level="all"/>
 <Appenders>
    <Appender type="Console" name="Out">
      <Layout type="PatternLayout" pattern="${dataPattern}"/>
    </Appender>

  <Appender type="RollingFile" name="Main" fileName="${logDir}/${moduleName}_main.log"
                   filePattern="${logDir}/${moduleName}_main.log.%i">
        <Layout type="PatternLayout" pattern="${dataPattern}"/>
        <Policies>
          <SizeBasedTriggeringPolicy size="8 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="10" fileIndex="min"/>
    </Appender>

    <Appender type="RollingFile" name="Err" fileName="${logDir}/${moduleName}_err.log"
                   filePattern="${logDir}/${moduleName}_err.log.%i">
        <Layout type="PatternLayout" pattern="${dataPattern}"/>
        <Filter type="ThresholdFilter" level="error"/>
        <Policies>
          <SizeBasedTriggeringPolicy size="8 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="10" fileIndex="min"/>
    </Appender>
 <Loggers>
      <Logger name="com.dsl.${moduleName}" level="${logLevel}">
          <AppenderRef ref="Main"/>
          <AppenderRef ref="Err"/>
      </Logger>
<Root level="${logLevel}">
        <AppenderRef ref="Main"/>
        <AppenderRef ref="Err">
            <Filter type="ThresholdFilter" level="error"/>
        </AppenderRef>      
    </Root>
  </Loggers>
</Configuration>

有错误:

Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.appender.RollingFileAppender, element RollingFile. java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory@61b3c057] unable to create manager for [${web:rootDir}/mod_main.log] with data [org.apache.logging.log4j.core.appender.rolling.RollingFileManager$FactoryData@1e722a46[pattern=${web:rootDir}/mod_main.log.%i, append=true, bufferedIO=true, bufferSize=8192, policy=CompositeTriggeringPolicy(policies=[SizeBasedTriggeringPolicy(size=8388608)]), strategy=DefaultRolloverStrategy(min=1, max=10), advertiseURI=null, layout=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{3} - %m%n%ex]]

ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile. java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender

以及:

ERROR Null object returned for RollingFile in Appenders.
ERROR Null object returned for RollingFile in Appenders.
ERROR Unable to locate appender "Main" for logger config "root"
ERROR Unable to locate appender "Err" for logger config "root"
ERROR Unable to locate appender "Main" for logger config "com.dfs.acs"
ERROR Unable to locate appender "Err" for logger config "com.dfs.acs"

1 个答案:

答案 0 :(得分:0)

对于Servlet 3.0,似乎是$ {web:rootDir}的解决问题。