Sajal Samaiya
Sajal Samaiya

Reputation: 1

websphere liberty - Not able to generate application specific logging using slf4j or log4j

How can I generate Application specific logs in IBM Websphare Liberty server, I am using SLF4j, the message.log and console.log is updating fine but the application specific logs are not getting generated. If the logging issue can be resolved using Log4j, then also will work for me.

Tried loading log4j2 file explicitly in static block and also placed in resource folder, both didnt worked.

able to see liberty server log but application logs are not generating at all.

Upvotes: 0

Views: 1883

Answers (2)

Sajal Samaiya
Sajal Samaiya

Reputation: 1

Blow fix worked for me, from the application class or entry point use below code to reload the slf4j configuration.

private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(theClassfromwheregeneratinglogs.class);

static {
    try {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    // Call context.reset() to clear any previous configuration, e.g. default 
    // configuration. For multi-step configuration, omit calling context.reset().
    context.reset(); 
    configurator.doConfigure("/appserver/wlpusr/servers/applogs/logback.xml"); //This will reload the slf4j configuration from the given file location.     
  } catch (JoranException je) {
    // StatusPrinter will handle this
  }}

Upvotes: 0

J.Frank
J.Frank

Reputation: 36

The root cause of not seeing log4j logs is usually because the log4j2 configuration file is not being picked up by the classloader. You have a few options here to solve your problem.

  1. Copy to Liberty shared-library directory. It can be one of the following:
    • ${shared.config.dir}/lib/global
    • ${server.config.dir}/lib/global

You can refer to IBM websites to find out the exact location of ${shared.config.dir} and ${server.config.dir} in your Liberty installation at here

  1. Alternatively, you can place the log4j configuration file anywhere on your file system and add the following lines to your server.xml
    <library id="log4jConfig">
          <folder dir="/{directory containning log4j config file}" scanInterval="5s" />
    </library>

    <webApplication id="myapp" location="myapp.war" name="My App"/>
          <classloader commonLibraryRef="log4jConfig"/>
    </webApplication>
  1. Set as a JVM argument inside the jvm.options file -Dlog4j.configurationFile=file:/path/to/log4j2.xml

  2. Package it inside your maven application war file under src/main/resources

Upvotes: 1

Related Questions