我正在尝试在Spring Web应用程序中从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被添加到classpath中:
> log4j-slf4j-impl-2.3.jar
> log4j-core-2.3.jar
> log4j-api-2.3.jar
> slf4j-api-1.7.12.jar
> commons-logging-1.1.1.jar
> 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>
解决方法:
您的RollingFile配置需要指定触发翻转的原因.您可以通过添加< Policy>元件.有关详细信息,请参见RollingFile manual entry.
我相信在< / RollingFile>结束之前添加此代码段.标签应解决此问题:
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
对于analyticsFileAppender RollingFile附加程序,您配置了一个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>