1

I have a simple question, hope I will get a simple answer.

I need a log4j2 xml which will dump ALL logs no matter where they are generated from. Now, funny thing is that, I see all the logs that I do not want to see, but logs from my file show up the dreaded "log4j:WARN No appenders could be found for logger".

My simple log xml file:

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

    <!-- Console Appender -->
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout
            pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
    </Console>

    <!-- File Appender -->
    <File name="File"
        fileName="./log/abc.log">
        <PatternLayout
            pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
    </File>
</Appenders>

<category name="com.abc.def.config.AppInitializer">
    <priority value="DEBUG" />
    <appender-ref ref="File" />
</category>

<category name="com.oli">
    <priority value="DEBUG" />
    <appender-ref ref="File" />
</category>

<Loggers>
    <Root level="trace">
        <AppenderRef ref="Console" />
        <AppenderRef ref="File" />
    </Root>
</Loggers>

Can somebody improve this xml file so that I am able to see the logs generated by my class "com.abc.def.config.AppInitializer" in the log file ?

Note, more logs is not bad for me, but missing logs absolutely not an option .. the ultimate goal is to "filter out messages that we do not need" rather than "filter in messages we need".

user1300830
  • 83
  • 1
  • 10

2 Answers2

1

The error message log4j:WARN No appenders could be found for logger is not a Log4j2 warning.

It is coming from a log4j-1.2.x jar that is still on the classpath somewhere.

When migrating to Log4j2, include the log4-1.2-api jar and make sure to remove any log4j-1.2.x jars from the classpath.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0

From the config file you provided this seems quite good. You should see your log messages on the console as well as in the file.

The warning you get at the very beginning already give you a hint - the system is not able to find your configfile. So how did you name it and where did you put it. The way log4j2 is looking for your configuration is the following:

  1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
  2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
  3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  5. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  6. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
  7. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  8. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  9. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  10. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

This one was stolen from Log4j2 documentation.

Hope that helps. If not feel free to post your code (github link would be nice) so we can check in more depth.

TobiSH
  • 2,833
  • 3
  • 23
  • 33