无法找到附加程序

Unable to locate appender

我正尝试在 spring 网络应用程序中从 log4j 1 迁移到 log4j 2。当我构建它时,出现以下错误:

2015-08-10 16:26:36,222 DEBUG Built Plugin[name=AppenderRef] OK from factory method.
2015-08-10 16:26:36,222 DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for builder factory method...
2015-08-10 16:26:36,222 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger. Going to try finding a factory method instead.
2015-08-10 16:26:36,223 DEBUG Still building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for factory method...
2015-08-10 16:26:36,223 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig$RootLogger.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2015-08-10 16:26:36,224 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(name="null", name="INFO", name="null", ={appFileAppender}, ={}, Configuration(/Applications/tomcat/webapps/ROOT/WEB-INF/classes/log4j2.xml), null)
2015-08-10 16:26:36,225 DEBUG Built Plugin[name=root] OK from factory method.
2015-08-10 16:26:36,225 DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for builder factory method...
2015-08-10 16:26:36,227 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggersPlugin. Going to try finding a factory method instead.
2015-08-10 16:26:36,228 DEBUG Still building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for factory method...
2015-08-10 16:26:36,228 DEBUG Found factory method [createLoggers]: public static org.apache.logging.log4j.core.config.Loggers org.apache.logging.log4j.core.config.LoggersPlugin.createLoggers(org.apache.logging.log4j.core.config.LoggerConfig[]).
2015-08-10 16:26:36,229 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.LoggersPlugin for element Loggers with params(={app, analyticsLogger, dpLogger, trackingPixelLogger, filteredLogsLogger, trackingOffersPurchasingLogger, ifbLogger, trackingOffersUdidLogger, trackingOffersHeadersLogger, apLogger, s3AdminLogger, bookPackLogger, accumulatorLogger, appDownloadLogger, botDetectionLogger, fourGLogger, urlShortenerLogger, TrackingAllLogger, otiHeadersLogger, postpaidErrorTrackingLogger, root})
2015-08-10 16:26:36,230 DEBUG Built Plugin[name=loggers] OK from factory method.
2015-08-10 16:26:36,235 ERROR Unable to locate appender appFileAppender for logger
2015-08-10 16:26:36,237 ERROR No appender named fourGFileAppender was configured

以下 jar 已添加到类路径中:

  1. log4j-slf4j-impl-2.3.jar
  2. log4j-core-2.3.jar
  3. log4j-api-2.3.jar
  4. slf4j-api-1.7.12.jar
  5. commons-logging-1.1.1.jar
  6. apache-log4j-extras-1.1.jar

我的 log4j2.xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">

    <Appenders>

        <RollingFile name="appFileAppender" fileName="/tmp/portal-fe.log"
                     filePattern="/tmp/portal-fe.%d{yyyy-MM-dd}.log.gz">
            <PatternLayout
                    pattern="%d{yyyy-MM-dd HH:mm:ss} [%p] | [%t] [%X{requestId}] [%X{x_msisdn}] [%X{x_rat}] [%X{x_forwarded_for}] [%X{circle}] [%C{1}:%L] %m%n"/>
        </RollingFile>


        <RollingFile name="analyticsFileAppender" fileName="/tmp/ops_bc_log"
                     datePattern="'-'yyyyMMdd">
            <PatternLayout
                    pattern="%d{yyyy/MM/dd HH:mm:ss} %m%n"/>
        </RollingFile>

        <Async name="ASYNC">
            <AppenderRef ref="appFileAppender"/>
        </Async>

        <Async name="ASYNC_2">
            <AppenderRef ref="analyticsFileAppender"/>
        </Async>

    </Appenders>

    <Loggers>

        <Logger name="app" level="debug">
            <AppenderRef ref="ASYNC"/>
        </Logger>

        <Logger name="analyticsLogger" level="info">
            <AppenderRef ref="ASYNC_2"/>
        </Logger>

        <Root level="info">
            <AppenderRef ref="appFileAppender"/>
        </Root>
    </Loggers>

</Configuration>

首先,移除Async appender,将AsyncLogger的appender-ref直接指向ProcessorLoggingFile。其次,您必须在 AsyncLogger.

上添加 includeLocation="true"

除了异步记录器之外还有一个异步附加程序没有帮助,在这种情况下可能是阻止 includeLocation 正常工作的原因。

编辑: CHECK THIS 问题以获得更多帮助。

您的 RollingFile 配置需要指定触发翻转的内容。您可以通过添加 <Policy> 元素来实现。有关详细信息,请参阅 RollingFile manual entry

我相信在结束 </RollingFile> 标记之前添加此代码段应该可以解决问题:

<Policies>
  <TimeBasedTriggeringPolicy />
</Policies>

对于 analyticsFileAppender RollingFile appender,您配置了一个 datePattern,您的意思是 filePattern。下面是固定的配置片段。

<RollingFile name="analyticsFileAppender" fileName="/tmp/ops_bc_log"
             filePattern="'-'yyyyMMdd">
  <PatternLayout pattern="%d{yyyy/MM/dd HH:mm:ss} %m%n"/>
  <Policies>
    <TimeBasedTriggeringPolicy />
  </Policies>
</RollingFile>