Setting up log4j2 in IBM BPM 8.5.x/8.6

Follow

Introduction

As log4j1 was discontinued in August 5th, 2015 then if you have a requirement to do some custom logging, it's recommended you use log4j2 instead, if you want to customize your logs in IBM BPM, namely having different log files from the ones that come with the product, such as SystemOut.log, for instance.

At BP3, we are committed to respond to our customers and some of their desires over IBM BPM, namely logging. We already have an explanation on how to set up log4j1. Now, this article refreshes some of the assumptions made in that article.

Which log4j2 files should be considered?

Currently, log4j is not visible as part of the product, so one needs to download it from Apache Logging Project here.

After downloading it, unzip it or untar it, depending on the downloaded version and consider only the following jar files, as the example here described concerns only file based logs:

  • log4j-api
  • log4j-core

At time this article is being written, Log4J is in the 2.10.0 version, so the files will be:

  • log4j-api-2.10.0.jar
  • log4j-core-2.10.0.jar

To use other logging strategies, such as writing in databases or other, please refer to Apache's Log4j2 documentation and see which other files you'll need in order to make it work.

Making log4j2 ready for IBM BPM

To better isolate and create a well known place for the referred jar files, please copy them into a location on your desire, which can be accessible by IBM BPM process owner, in order to avoid any permission issues.

In our case, the files were copied into /opt/apache/log4j2

So, at WAS Console (https://yourservername:9043/ibm/console) go to:

Environment > Shared Libraries and select IBM_BPM_Process_Server_Shared_Library

Screen_Shot_2018-01-04_at_21.16.03.png

Here, add the location of the log4j2 files at the ClassPath text box, like (don't forget the final /):

Screen_Shot_2018-01-04_at_21.19.20.png

After doing it restart your Single Cluster or your Application Cluster, depending on your BPM topology and log4j2 is ready to be used inside BPM.

Calling log4j2 inside your Process Application or Toolkit

Now that log4j2 is available in your BPM Server, you can call it using LiveConnect.

For instance:

Packages.org.apache.logging.log4j.LogManager.getLogger("yourlogger").info("yourmsg");

Log4j configuration

There are different ways of setting up your loggers.

Using the file based approach or using the programmatic approach. For the sake of the example, we're going to describe the file based approach. If you're interested in the programmatic approach, please check Extending Log4j or Programmatic Configuration, in Apache Log4j documentation.

 

Log4j2 simplifies its configuration using an automatic approach with a given verification order.

Basically, one needs to create a log4j2.xml file as the following and make it available in the Classpath*:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="CustomFile1" fileName="/tmp/customFile1.log" immediateFlush="true" append="true">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
        <File name="CustomFile2" fileName="/tmp/customFile2.log" immediateFlush="true" append="true">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
        </Root>
        <logger name="custom1" level="INFO">
            <AppenderRef ref="CustomFile1"/>
        </logger>
        <logger name="custom2" level="INFO">
            <AppenderRef ref="CustomFile2"/>
        </logger>
    </Loggers>
</Configuration>

In the example we can see the existence of three different appenders - one for the console and two other to write to a given file.

By default, every message is being written to the console - as described in the Root Logger, but the other appenders can be called up using "custom1" or "custom2", as Loggers, like: 

Packages.org.apache.logging.log4j.LogManager.getLogger("custom1").info("HELLO1!");
Packages.org.apache.logging.log4j.LogManager.getLogger("custom2").info("HELLO2!");

One can set different log levels and different loggers changing this same file.

For the sake of the current example, the result of the previous two calls will be:

  • At the console:
[1/2/18 19:42:33:625 CET] 00000136 SystemOut     O 19:42:33.619 [WebContainer : 1] INFO  custom1 - HELLO1!
[1/2/18 19:42:33:639 CET] 00000136 SystemOut     O 19:42:33.638 [WebContainer : 1] INFO  custom2 - HELLO2!
  • At /tmp/customFile1.log:

[2018-01-02 19:42:33.619] [WebContainer : 1] INFO custom1 - HELLO1!

  • At /tmp/customFile2.log:

[2018-01-02 19:42:33.638] [WebContainer : 1] INFO custom2 - HELLO2!

 

Note:

* Please note that any change done to your log4j2.xml file forces you to restart your Application Cluster or your Single Cluster

Have more questions? Submit a request

Comments

Powered by Zendesk