7

I am trying to migrate from log4j 1 to log4j 2 in a spring web application. When I build it I get the following error:

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

Following jars are added to classpath:

  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

And my log4j2.xml looks like:

<?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>
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Antara Roy
  • 475
  • 1
  • 7
  • 14
  • possible duplicate of [No appenders could be found for logger(log4j)?](http://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j) – Ankur Mahajan Aug 10 '15 at 11:42
  • I checked the [question](http://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j) but mine is different. I have properly added my log4j2.xml in the classpath and it is available in `/WEB-INF/classes/`. I guess I have configured the `log4j2.xml` incorrectly. – Antara Roy Aug 10 '15 at 12:02

2 Answers2

8

Your RollingFile configuration needs to specify what triggers a rollover. You do this by adding a <Policy> element. See the RollingFile manual entry for details.

I believe adding this snippet before the closing </RollingFile> tag should fix the issue:

<Policies>
  <TimeBasedTriggeringPolicy />
</Policies>

For the analyticsFileAppender RollingFile appender you configured a datePattern where you meant to say filePattern. Below is the fixed config snippet.

<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>
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • I added `Policies` and `filePattern` like [this](https://gist.github.com/anonymous/38bbfc3dfc4861907e8e). Is it correct ? – Antara Roy Aug 10 '15 at 12:44
  • Did it fix the problem? – Remko Popma Aug 10 '15 at 12:45
  • It is giving me this [error](https://gist.github.com/ramswaroop/78eaac3e311863f605e3). – Antara Roy Aug 10 '15 at 12:47
  • 1
    Config looks correct. I don't actually see any error, it looks like your app exits immediately. Can you try a simpler config first? Looks like you have 20 or so async appenders set up. Can you try with just one or two RollingFileAppenders first, make that work, then gradually add – Remko Popma Aug 10 '15 at 12:56
  • I removed all loggers from [log4j2.xml](https://gist.github.com/ramswaroop/aab62f86d53b97f19c5d) except first one and here is the [error log](https://gist.github.com/ramswaroop/e4625610cb051cd25f8c). – Antara Roy Aug 10 '15 at 13:06
  • Your config looks okay. You could simplify further by removing the named logger and make the root logger level DEBUG and point its AppenderRef to the ASYNC appender. However, I don't think that's the problem. What does your application do? It doesn't exit immediately, does it? Does your portal-fe.log file show anything? – Remko Popma Aug 10 '15 at 13:47
  • See [Portal-fe.log](https://gist.github.com/anonymous/987f51bd921572cb6508), I guess the problem lies here. – Antara Roy Aug 10 '15 at 13:57
  • One more thing, along with the 4 jars mentioned in the question, I also have `commons-logging-1.1.1.jar` and `apache-log4j-extras-1.1.jar` in my classpath. Is it okay? – Antara Roy Aug 10 '15 at 14:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86618/discussion-between-antara-roy-and-remko-popma). – Antara Roy Aug 10 '15 at 14:07
  • 1
    Extras is for log4j-1.2, not Log4j2. You can try adding the log4j-1.2-api bridge that is part of log4j2. This routes all log4j-1.2 calls to log4j2. – Remko Popma Aug 10 '15 at 14:18
0

First, remove the Async appender, and point the appender-ref of the AsyncLogger to the ProcessorLoggingFile directly. Second, you must add includeLocation="true" on the AsyncLogger.

Having an async appender in addition to an async logger does not help and in this case might be what prevents the includeLocation from working correctly.

EDIT: CHECK THIS question for more help.

Community
  • 1
  • 1
Ankur Mahajan
  • 3,396
  • 3
  • 31
  • 42
  • Where did you place your log4j.xml. This problem might be occurred if you haven't added in the class-path. So placed log4j.xml or log4j.properties file in class path (/WEB-INF/classes/). i.e , WEB-INF/classes/log4j.xml – Ankur Mahajan Aug 10 '15 at 11:38
  • I am getting this error too: `ERROR RollingFile contains an invalid element or attribute "datePattern"`. How do I specify the rollover strategy then? I guess through `filePattern` ? – Antara Roy Aug 10 '15 at 12:05
  • Please refer this for date Patterns 'http://www.codejava.net/coding/configure-log4j-for-creating-daily-rolling-log-files' – Ankur Mahajan Aug 10 '15 at 12:30