Search code examples
javaopenshiftlog4j2java.util.loggingopen-liberty

log4j-to-slf4j not picking up log4j2.xml


Trying to deploy a liberty application to OCP and merge the liberty logging with the application logging per example: https://openliberty.io/blog/2020/05/19/log4j-openshift-container-platform.html

Our application uses LOG4J2 to configure and run the logging. This worked sofar with following dependencies.

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>

and use the following line of code to tell the app where the log4J2.xml file is located

Configurator.initialize(null, log4jConfigUrlFile);

our logging ended up in the logfiles and everything was fine. However now we need some way to use Log4J2 to configure our loggers but the logging itself must end up in JUL so it can be used by OCP and their EFK stack.

I have been trying with the following dependencies.

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-to-slf4j</artifactId>
  <version>2.13.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.7</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

There are no compile issues and the program starts and I see logging appear in the console. However when I try tweaking our log4j2.xml (who is on the classpath) and for example don't want to see the info messages they still appear. it seems it's not loaded or not used. Another thing that I notice is that the LoggingFactory is the one from slf4j-jdk14 maybe that is an issue. We also can't use the Configurator anymore since that is part of log4j-core and you can't have two implementations (log4j-to-slf4j) on the same classpath.

I also tried setting the logfile location property

-Dlog4j.configurationFile=./resources/log4j/log4j2.xml

I tried this with several permutations of directory structure just to be sure and isn't working. Also tried the following.

-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE

Just to get some more debug output but haven't gotten that even to work. Is there any other thing I can try ?

My concrete endgoal is

  • Configure logging by means of log4j2.xml
  • Have logging end up as java.util.logging in the console

Solution

  • Unfortunately, I believe if you use the log4j-to-slf4j adapter, log4j2.xml configuration will be ignored. Any logging configurations must be changed on your server side in your server.xml. In the case of filtering your logs based on log level, you can use the consoleLogLevel attribute. In your bootstrap.properties file:

    com.ibm.ws.logging.console.log.level=<log level>
    

    Or in your server.xml file:

    <logging consoleLogLevel="<log level>" />
    

    Or in your server environment:

    WLP_LOGGING_CONSOLE_LOGLEVEL=<log level>
    

    Valid options for log level include AUDIT,ERROR,INFO,OFF,WARNING. Please refer to https://openliberty.io/docs/20.0.0.11/reference/config/logging.html for more information about how to configure logging in Open Liberty. Alternatively, you can use the other options stated in the blog to send your logs to OCP.

    EDIT: Another option you could do is use the Console appender instead. You can send your application logs to console directly in non-json format. If you want to keep Liberty logging in JSON and keep your log4j 2 application logs in JSON, you can also use appsWriteJson logging attribute to send your JSON application logs directly to System.out/err.

    bootstrap.properties:

    com.ibm.ws.logging.apps.write.json=true
    

    server.xml

    <logging appsWriteJson="true"/>
    

    server environment:

    WLP_LOGGING_APPS_WRITE_JSON=true
    

    Console Appenders: https://logging.apache.org/log4j/2.x/manual/configuration.html#Appenders More info about appsWriteJson: https://openliberty.io/blog/2020/07/30/json-logging-open-liberty-20008.html