0

title says it all. How do I setup a log4j2 configuration in my Open-liberty project? I've added my log4j2.xml file in the resources folder and I use the following dependency in my pom.xml:

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

This is my server.xml:

<server description="Intake Server">
    <featureManager>
        <feature>servlet-4.0</feature>
        <feature>mpConfig-1.4</feature>
        <feature>cdi-2.0</feature>
    </featureManager>

    <variable name="default.http.port" defaultValue="9080"/>
    <variable name="default.https.port" defaultValue="9443"/>
    <variable name="app.context.root" defaultValue="message"/>


    <httpEndpoint httpPort="${default.http.port}" 
    httpsPort="${default.https.port}" id="defaultHttpEndpoint"  host="*" />

    <library id="log4jConfig">
          <folder dir="/var/log/intake" scanInterval="5s" />
    </library>


    <webApplication id="intake" location="intake.war" contextRoot="${app.context.root}">
        <classloader commonLibraryRef="log4jConfig"/>
    </webApplication>
</server>
BrentL
  • 47
  • 1
  • 10

1 Answers1

0

You'll also need to add log4j to the classpath, and there's several ways you can do that (with pros/cons to each, but I would suggest #2, unless you have more then one app that's going to be making use of log4j, in which case 3 may be the better approach):

  1. Package it with your app (inside the war)
  2. Add it in the server.xml via the classloader attribute under your webApplication
  3. Drop it in the global shared library folder: ${shared.config.dir}/lib/global or ${server.config.dir}/lib/global
  4. Set it via JVM argument: -Dlog4j.configurationFile=file:/path/to/log4j2.xml

I'll mention that there is also an archived repo regarding using log4j with Open Liberty that you may find helpful, but keep in mind that it is archived so it's likely incomplete and with no dev support.

M. Broz
  • 704
  • 4
  • 11
  • Thank you for the response! I followed the tutorial provided in the repository but it unfortunately doesn't work. I tried the second option as you suggested. It finds my log4j2.xml file but doesn't implement it and LogManager.getLogManager().getLogger(CLASS); returns null. – BrentL Aug 27 '21 at 09:07
  • can you update the question to show your server.xml snippet with what you added for #2? – M. Broz Aug 27 '21 at 13:33
  • I updated my answer and added the server.xml file. – BrentL Aug 30 '21 at 06:47
  • I'm still having this issue. I updated my question with the server.xml file, if you could give a quick look I would really appreciate it! – BrentL Sep 27 '21 at 07:22