Thursday, September 1, 2011

Custom logging in BPEL 2.0 using Log4j

We can use Java Embedding activity to do custom logging using log4j.
Download log4j
Extract the zip file and use the log4j.jar file from that.There are two approaches of using the jar file.
First approach is to put the jar file at the server so that all the applications can use it.
For  doing to do the following steps.
1. Copy the log4j,jar at the following location  /Oracle_SOA1/soa/modules/oracle.soa.ext_11.1.1/
2. Open the command prompt and go the the above location.
3. Set the following enironment variables
ANT_HOME -where your ant is installed
PATH -path to bin folder of ant
JAVA_HOME -path to jdk
4.Run the ant command.
5. Check the oracle.soa.ext.jar file in the path log4j.jar path gets added in the jar file
6. Restart the managed server.
Now create log4j.properties file
 # logging: Log4j_BPEL
log4j.category.Log4j_BPEL=info,Log4j_BPEL
log4j.appender.Log4j_BPEL=org.apache.log4j.RollingFileAppender
log4j.appender.Log4j_BPEL.File=D:\\Log4j_BPEL.log
log4j.appender.Log4j_BPEL.MaxFileSize=5MB
log4j.appender.Log4j_BPEL.MaxBackupIndex=10
log4j.appender.Log4j_BPEL.layout=org.apache.log4j.PatternLayout
log4j.appender.Log4j_BPEL.layout.ConversionPattern=%d{ISO8601} %-5p [%t] [%c] [%x] %m%n
Copy this file in any location on the server.Say D:\log4j.properties
Import the classes in .bpel file for bpel 2.0
          For importing the classes in bpel 1.1 use the following


importType="http://schemas.oracle.com/bpel/extension/java"/>


importType="http://schemas.oracle.com/bpel/extension/java"/>

Now in your java embedding activity enter the following java code
/*Write your java code below e.g.            
                System.out.println("Hello, World");           
*/           
try{    
String sLog4jFile = "D:\\log4j.properties";             
PropertyConfigurator.configure(sLog4jFile);             
Logger logger = Logger.getLogger("Log4j_BPEL");      
logger.info(">>>> Message log for instance: " + getTitle());         
XMLElement input= (XMLElement)getVariableData("inputVariable", "payload","/client:process/client:input");         
addAuditTrailEntry("input is: " + input.getTextContent());        
logger.info("Input Value :"+input.getTextContent());       
setVariableData("outputVariable","payload","/client:processResponse/client:result", input.getTextContent());    
}catch(Exception e)    
{    
e.printStackTrace();    
}

Deploy the composite and check log file gets created in the location mentioned in the properties file.
Other approach is to copy the log4j.jar in the composite folder under /SCA-INF/lib folder.

6 comments:

  1. Thank you very much for this outstandingly clear and concise tutorial. you'r my hero!

    ReplyDelete
  2. Nice One. I had one doubt here. Is it possible to enable/disable this log4j logging on demand(without restart/redeploying the app especially in production). Expecting something like admin configurations. If YES please let me know how to configure this.

    Looking forward to hear from you ASAP.

    Thanks,
    Swamy.

    ReplyDelete
  3. nice one. please change the code for importing the classes as below.



    Thanks
    Ritesh

    ReplyDelete
    Replies
    1. import location="oracle.xml.parser.v2.XMLElement" importType="http://schemas.oracle.com/bpel/extension/java"

      Delete
  4. where will i add the import?

    ReplyDelete
  5. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Your write up is a fine example of it.

    Oracle Fusion SCM Online Training

    ReplyDelete