BPM, Workflow, and Case

BPM, Workflow, and Case

Come for answers. Stay for best practices. All we’re missing is you.

 View Only

IBM BAW - Custom Logging using built-in java.util.logging.Logger

  • 1.  IBM BAW - Custom Logging using built-in java.util.logging.Logger

    Posted Thu May 08, 2025 02:53 PM
    Edited by Muhammad Haris Khan Sun May 11, 2025 04:24 AM
    @Grant Taylor thank you for giving the clue you have given here BAW Custom logging | BPM, Workflow, and Case.

    The custom logger demonstrated in above document is using java.util.logging.Logger which is built-in in IBM BAW v24.0.1. Using this approach, the custom logs will be printed on a separate file for each process app.

    For this purpose, there are 2 files which are created i) CustomLogger.js ii) MyFormatter.java. Pasting the code below for these 2 files:

    1. Create a toolkit and add the environment variable "log4jPath" and put some default value which will hold the folder path where the log files will be created. For e.g., "C://IBM/Workflow/CustomLogger/logs/"

    2. Include below CustomLogger.js in the toolkits (created above) under the Server Files.

    Note: On line number 4 in CustomLogger.js, FileHandler is initialized with 3 parameters viz., file name, file size (20 MB) and number of files to be kept i.e., 30.
     
    CustomLogger.js:
    function customLogger(level, message) {
      var acr = tw.system.model.processApp.acronym; // Finding the process acronym
      var myLog = Packages.java.util.logging.Logger.getAnonymousLogger();
    
      var fileHandler = new Packages.java.util.logging.FileHandler(tw.env.log4jPath + acr + "_%u_%g.log", 20000000, 30, true);
      var formatter = new Packages.com.gbm.ibm.MyFormatter();
      fileHandler.setFormatter(formatter);
      myLog.addHandler(fileHandler);
    
      // if setUseParentHandlers set to false then the myLog logger will NOT print the log to SystemOut.log file and will only print the log to the filename provided above in FileHandlermyLog.setUseParentHandlers(false);
    
      var taskName = (tw.system.currentTask == null) ? "" : ">" + tw.system.currentTask.subject;
      var instanceID = (tw.system.currentProcessInstanceID == null) ? "" : ">" + tw.system.currentProcessInstanceID;
      var logMsg = "[" + tw.system.model.processApp.acronym + taskName + instanceID + "]::" + message;
    
      if ("info" == level) {
        myLog.info("INFO::" + logMsg);
      } else if ("debug" == level) {
        myLog.fine("FINE::" + logMsg);
      } else {
        myLog.severe("SEVE::" + logMsg);
      }
    
      fileHandler.close();
    }
    
    function customObj(logData) {
      this.info = function(logData) {
        customLogger("info", logData);
      }
    
      this.debug = function(logData) {
        customLogger("debug", logData);
      }
    }
    
    var logMsg = new customObj();

    3. Create a Java Project in Eclipse or other IDE, then add below class (MyFormatter.java) in it

    4. Build and export the Java Project to a jar file (let's say name it CustomLogger.jar).

    5. Upload the CustomLogger.jar file in the same toolkit under the Server Files.

     
    MyFormatter.java
    public class MyFormatter extends Formatter {
    
        @Override
        public String format(LogRecord record) {
            return record.getThreadID()+"::"
    //        		+record.getSourceClassName()+"::"
    //                +record.getSourceMethodName()+"::"
                    +new Date(record.getMillis())+"::"
                    +record.getMessage()+"\n";
        }
        
    }
    

    6. Take a snapshot of the toolkit.

    7. Include the toolkit in any of the process apps or in multiple process apps. 

    8. Testing:

    a)      Then create a Service Flow in one of the above process apps. And add the code below in a Script of Service Flow.

    logMsg.info("Hello testing logMsg 113");
    logMsg.error("Hello testing logMsg 112");

    b)      Execute the Service Flow and it will print the above logs in the desired folder and the desired log file.


    ------------------------------
    Muhammad Haris Khan
    ------------------------------