Friday, August 31, 2012

Using Custom Log Files with Log4j - BPEL

In day to day development, every application requires logging of data/errors in the log files.
In BPEL code development, we may use the following lines in a Java Embedding activity to log texts and errors in the Server log files (i.e servername-diagnostic.log)

com.collaxa.ServerLogger.error("SOA_5007", "Severity :3", "SFO-SOA_5007 generic process failed");   
java.lang.String ErrorMsg = (String)getVariableData("ErrorMessage");       
com.collaxa.ServerLogger.error(ErrorMsg);]

But a server log contains a clutter of information apart from our application specific logged messages. It would be a good thing to do if we can have a custom log file for our application specific logging.

Log4j is a wonderful logging API, which we are going to use in our implementation. We are going to have 2 step configuration  - 
 - Environment Specific Configurations
 - Application Specific Configurations

Environment Specific Configurations
1. There are two ways of adding a custom jar for use in your application  - 
a. Copy log4j-1.2.16.jar to //soa/modules/oracle.soa.ext_11.1.1 and execute ANT script in this directory. This will recreate the oracle.soa.ext.jar file adding the log4j lib to the classpath.

Prerequisites:  

Option 1
a) cd  to directory of oracle.soa.ext_11.1.1 
b) >  C:\\\modules\org.apache.ant_1.7.1\bin\ant (in windows)
bash$  ./u01//modules/org.apache.ant_1.7.1/bin/ant (in Unix)

Option 2

In WINDOWS

In environmental variables, set 
ANT_HOME  = F:\oracle\Middleware\modules\org.apache.ant_1.7.1
Path = %ANT_HOME%\bin

Then from cmd go the directory and type ant and hit enter.

In UNIX

For C shell (csh), edit the startup file (~/.cshrc): 
set path=(/usr/local/jdk1.6.0/bin )

For bash, edit the startup file (~/.bashrc): 
PATH=/usr/local/jdk1.6.0/bin:
export PATH

For ksh, the startup file is named by the environment variable, ENV. To set the path: 
PATH=/usr/local/jdk1.6.0/bin:
export PATH

For sh, edit the profile file (~/.profile): 
PATH=/usr/local/jdk1.6.0/bin:
export PATH

Then load the startup file and verify that the path is set

b. Place the jar in /user_projects/domains/domainName/lib

2. Create a new folder for Log4j configuration and log files like /Log4jConfigFiles

3. Add to this folder the configuration files (log4jConfig.xml and log4j.dtd, for example)

4. Set the log file name and path (/Log4jConfigFiles/customErrorLog.log, for example) in the log4jConfig.xml

5. Change the setDomainEnv script(.cmd for windows or .sh for Unix) found in /user_projects/domains//bin to point to Log4j configuration file:

In Unix
JAVA_OPTIONS="-Dlog4j.configuration=file /Log4jConfigFiles/log4jConfig.xml" 

In Windows
set JAVA_OPTIONS=-Dlog4j.configuration=file:"/Log4jConfigFiles/log4jConfig.xml"

6. Restart the SOA managed server. 

Application Specific Configurations

1. Import  log4j-1.2.16.jar to the project libs (right click in the project name and select “Project Properties”, go to “Libraries and Classpath” and add the jar file)

2. Insert a Java Embedding activity

3. In the BPEL source code, import the Logger class adding the code below before the “bpelx:exec” created for the Java Embedding component


4. Edit the Java Embedding activity and insert the necessary log instructions like to test the functionality:

Logger mLogger = Logger.getLogger("myCustomLog");  
mLogger.info(" >>>>>>>>> This is my first log4j statement ****************");  
mLogger.debug(">>>>>>>>>>>>>>>Logger Debug Stmt");  
mLogger.error(">>>>>>>>>>>>>>>Logger Errorrr"); 
java.lang.String ErrorTrace = ((org.w3c.dom.Element)getVariableData("inputVariable","payload","/client:process/client:input")).getFirstChild().getNodeValue(); 
mLogger.info("erroorrr trace ::::"+ErrorTrace); 
mLogger.info(ErrorTrace);

All the files mentioned in this post can be downloaded from here .

You can also find a working SOA project inside the downloaded zip named "TestCustomLog.zip" implementing the above described functionality.


________________
thanks, D