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:
- Service wise log file implementation.
- Log4j implementation in OSB.
- JAVA call out mechanism for custom log implementation.
- VM Argument uses for log file location for making it domain independent (one time configuration for each domain).
Implementation steps:
- Stop the server and it’s all nodes (If it's cluster)
- Copy the file “log4j-config.xml” in %DOMAIN_HOME/BIN% or any other location.
- Copy the jar file “log4j-1.2.16.jar” in %DOMAIN_HOME/BIN% or any other location.
- Open the file “setDomainEnv” in edit mode.
- 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>
<!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:
- Create a java project.
- Add log4j.jar in it's build path.
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");
}
}
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



OSB Configuration:
- Create a OSB configuration project
- Create a OSB project
- Create a proxy, Business service.
- Create a folder inside OSB project with name "JAR"
- Copy the generated JAR file of java project in this JAR folder.
- Open the proxy service message flow.
- Add the JAVA call out as per your need in message flow.
- Click on java callout, Select the jar file from the JAR folder.
- 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).
- 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.
- We have done with the OSB Part also, Deploy the service and see the result.
Hope you will enjoy to have it!!!
Comments
Post a Comment