Skip to main content

OSB Custom Logs with log4j

Hi Reader,

Today i am discussing with one common need of all developers i.e logs implementation as we all know OSB provides a log component which can be used inside any proxy service or in split join for logging. OSB Log component has some limitation like it write logs in weblogic admin server log file, if some how user want to modify default OSB logging it will impact complete domain of weblogic.

For such use cases implementation where custom logging is required we can follow this approach.

Use Case:
  1. Service wise log file implementation.
  2. Log4j implementation in OSB.
  3. JAVA call out mechanism for custom log implementation.
  4. VM Argument uses for log file location for making it domain independent (one time configuration for each domain).
Implementation steps:
  1. Stop the server and it’s all nodes (If it's cluster)
  2. Copy the file “log4j-config.xml” in %DOMAIN_HOME/BIN% or any other location.
  3. Copy the jar file “log4j-1.2.16.jar” in %DOMAIN_HOME/BIN% or any other location.
  4. Open the file “setDomainEnv” in edit mode.
  5. Add following line in top of the file after comments.
set JAVA_OPTIONS=-DlogXMLLocation=C:\Oracle_Soa\Middlewaare\home11g\user_projects\samples\domains\servicebus\bin\log4j-config.xml %JAVA_OPTIONS%
set CLASSPATH= C:\Oracle_Soa\Middlewaare\home11g\user_projects\samples\domains\servicebus\bin \log4j-1.2.16.jar;%CLASSPATH%

Here -DlogXMLLocation is a user defined VM property which is used for getting log4j configuration XML file As when we make a java call out from OSB it required to be this path either in class path (it will not work) so set that file location as fetched with help of VM arguments.
set CLASSPATH is for setting log4j.jar in classpath of OSB weblogic server so once OSB server will start it is able to call Log4j class at time of callout.
log4j-config.xml Sample:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC
  "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.EnhancedPatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601}{GMT-0500} [%-5p] [%t] [%F:%L] - %m%n" />
        </layout>
    </appender>

    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="${logfilename}.log" />
        <param name="Append" value="true" />
        <param name="MaxFileSize" value="5MB" />
        <!-- Keep one backup file -->
        <param name="MaxBackupIndex" value="100" />
        <layout class="org.apache.log4j.EnhancedPatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601}{GMT-0500} [%-5p] [%t] [%F:%L] - %m%n" />
        </layout>
    </appender>

    <logger name="org.apache.commons.beanutils.converters">
        <level value="info" />
    </logger>
    <logger name="net.sf.jasperreports">
        <level value="info" />
    </logger>

    <root>
        <priority value="debug" />
        <appender-ref ref="console" />
        <appender-ref ref="fileAppender"/>
    </root>

</log4j:configuration>
In this XML File ${logfilename} is the name of log file with it's logging location that will be passed from the JAVACallout to custom java class for logging.
Custom Logging implementation:
  1. Create a java project.
  2. Add log4j.jar in it's build path.
  3. LoggerUtil.java:
package com.oracle.service.bus.ServiceBusLogger;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;


public class LoggerUtil {

    final static Logger logger = Logger.getLogger(LoggerUtil.class);

    public static void logProxyIncio(String proxyName, String fileLocationName) {
        String logMessage = "Incio Proxy WS ... " + proxyName + "  ";
        logFlowInfoLevel(logMessage, fileLocationName);
    }

    public static void logProxyFinal(String proxyName, String fileLocationName) {
        String logMessage = "Final Proxy WS ... " + proxyName + "  ";
        logFlowInfoLevel(logMessage, fileLocationName);
    }

    public static void logProxyBusinessPLIncioCall(String PLName,
            String fileLocationName) {
        String logMessage = "Incio Procedure Call ... " + PLName + "  ";
        logFlowInfoLevel(logMessage, fileLocationName);
    }

    public static void logProxyBusinessPLFinalCall(String PLName,
            String fileLocationName) {
        String logMessage = "Final Procedure Call ... " + PLName + "  ";
        logFlowInfoLevel(logMessage, fileLocationName);
    }

    public static void logProxyBusinessWSIncioCall(String WSName,
            String fileLocationName) {
        String logMessage = "Incio Web Service Call ... " + WSName + "  ";
        logFlowInfoLevel(logMessage, fileLocationName);
    }

    public static void logProxyBusinessWSFinalCall(String WSName,
            String fileLocationName) {
        String logMessage = "Final Web Service Call ... " + WSName + "  ";
        logFlowInfoLevel(logMessage, fileLocationName);
    }

    private static void logFlowInfoLevel(String logMessage,
            String fileLocationName) {
        try {
            /*Setting log file name in system argument so same can be fetch for log file*/
            System.setProperty("logfilename", fileLocationName);
            /*Getting log file XMl configuration path from system argument so same can be fetch for logs*/
            String logFileConfg=System.getProperty("logXMLLocation");
            System.out.println("logFileConfg>>>>>>> "+logFileConfg);
            DOMConfigurator
                    .configure(logFileConfg);
            logger.info(logMessage);
        } catch (Exception e) {
            System.out.println("Their is some error in log file configuration");
        }
    }

    public static void main(String[] args) {
        logProxyIncio("Proxy3", "D:/OSBLogs/Proxy3");
        logProxyBusinessPLIncioCall("Proxy3",
                "D:/OSBLogs/Proxy3");
        logProxyBusinessPLFinalCall("Proxy3",
                "D:/OSBLogs/Proxy3");
        logProxyBusinessWSIncioCall("Proxy3",
                "D:/OSBLogs/Proxy3");
        logProxyBusinessWSFinalCall("Proxy3",
                "D:/OSBLogs/Proxy3");
        logProxyFinal("Proxy3", "D:/OSBLogs/Proxy3");
    }

}
Test this java class from VM property from cmd using Java -D or with eclipse with VM argument.
Click on java File >> Run As >> Run Configuration
See the result.
See log file in the folder that you provided in main method.
If result is fine as per you need, wrap this java project in the JAR file.
OSB Configuration:
  1. Create a OSB configuration project
  2. Create a OSB project
  3. Create a proxy, Business service.
  4. Create a folder inside OSB project with name "JAR"
  5. Copy the generated JAR file of java project in this JAR folder.
  6. Open the proxy service message flow.
  7. Add the JAVA call out as per your need in message flow.
  8. Click on java callout, Select the jar file from the JAR folder.
  9. Select the "LoggerUtil" class from the JAR and it's method which you want to call(make sure java call out works only with static methods).
  10. As per this LoggerUtil class, each method accept two arguments, one for the text that need to be print and other is the location for file name.
  11. We have done with the OSB Part also, Deploy the service and see the result.
Hope you will enjoy to have it!!!

Comments

Popular posts from this blog

Oracle SOA 12C rest adapter with Custom HTTP headers

Most existing web applications are connected through web services, which are commonly known as SOAP services. More and more users are relying on mobile devices for communication, and they’re looking for lighter ways to access enterprise information on the go. REST services are the answer for the mobile device platform, because they get rapid responses and fast access to data. Oracle SOA Suite 12 c  provides a complete set of service infrastructure components for designing, deploying, and managing composite applications. Oracle SOA Suite 12 c  enables services to be created, managed, and orchestrated into composite applications and business processes. Some time we have need to send HTTP headers in REST service, In OSB we use header component and add what ever is needed but in oracle SOA 12C it's little bit different. Let see how we can do it. Create one SOA Application. Create one SOA Sample project inside SOA Application. Go to composite and drag drop REST ...

Solution for BPM standard dashboard & activity guide not working in Oracle SOA 12.2.1.0 C

As earlier i publish a post about different issue of Oracle BPM, After some oracle support i got to fixed them. Issue Blog Here... Issue 3:BPM 12.2.1 process workspace activity guide not working. if you have a normal BPM Process, then this is the issue. In order to have data populated in "Activity Guide" firstly you should create a BPMN Guided Business Process. Below you can find the documentation about activity guide and how to create a Guided Business Process: https://docs.oracle.com/middleware/1221/bpm/bpm-develop/GUID-F765955D-90A5-48D4-8D2A-2F01FBB539E3.htm#BPMPD901 And here is specified: " A Guided Business Process is modeled as an activity guide that is based on a business process. The Activity Guide includes a set of Milestones. A milestone is a contained set of tasks that the end user has to complete. A milestone is complete when the user successfully runs a specific set of tasks in the milestone. " And in the below chapters you will find the ...

Swagger API document from Any WADL & Schema in Oracle SOA

Hi everyone, Hope everyone is doing well these days, Recently i started a project work over how to generate swagger API document for your any REST API, In case if you don't know what is swagger please go and check " https://swagger.io/tools/swagger-editor/ ". It's a great and easy to use tool which will help to create user friendly, human readable form API documentation with extension for generating API client in different languages with capability of testing your API from same. What is swagger editor, Design, describe, and document your API on the first open source editor fully dedicated to OpenAPI-based APIs. The Swagger Editor is great for quickly getting started with the OpenAPI (formerly known as the Swagger Specification) specification, with support for Swagger 2.0 and OpenAPI 3.0.  What benefits you will get by using swagger, Runs Anywhere, The Editor works in any development environment, be it locally or in the web. Smart Feedback, Validate you...