Sunday, July 31, 2011

OSB and Log4J

Objective: enable custom logging from OSB with Log4J

Libraries:
in C:\Oracle\Middleware\Oracle_OSB1\lib\external\log4j_1.2.8.jar you find the libraries

OSB Logger code:

package com.acme.osb;

public class OSBLogger {
 static java.util.logging.Logger loggerJava = java.util.logging.Logger.getLogger(OSBLogger.class.getName());
 static org.apache.log4j.Logger loggerLog4j = org.apache.log4j.Logger.getLogger(OSBLogger.class.getName());
 
 public static String logMessage(String message) {
  String messageout = "the message is " + message;
  System.out.println(messageout);
  loggerJava.log(java.util.logging.Level.SEVERE, messageout);
  loggerLog4j.debug("log4j" + messageout);
  return messageout;
 }
 
 public static String reloadConfiguration(String configFilename) throws Exception {
  String messageout = "reloading configuration from " + configFilename;
  System.out.println(messageout);
  URL url = Thread.currentThread().getContextClassLoader().getResource(configFilename);
  if (url == null) {
   throw new IllegalArgumentException("unable to locate resource " + configFilename);
  }
  String file = url.getFile();
  System.out.println("file=" + file);
  BufferedReader in = new BufferedReader(new FileReader(file));
  String text = "";
  while (in.ready()) {
   text = in.readLine();
   System.out.println(text);
  }
  //LogManager.resetConfiguration();
  PropertyConfigurator.configure(url);
  PropertyConfigurator.configureAndWatch(file);
  System.out.println("done reloading");
  return messageout;
 }
 
 
}



Extra configuration:

put your log4j.xml file in $DOMAIN_HOME/config/osb
(another example here)


Log4j 1.2.13 source here

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html

"The hierarchy will be reset before configuration when log4j.reset=true is present in the properties file. " but this is available only for log4j 1.2.15

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd

This great post on logging best practices

See http://forums.oracle.com/forums/thread.jspa?threadID=932901


My conclusion: configureAndWatch doesn't simply work. I am fed up investigating on this issue. Probably upgrading to the latest version of log4j would help.

I had to add an extra logger:

<logger name="org.apache.commons" additivity="false">
<level value="WARN"/>
<appender-ref ref="FILEAPACHE"/>
</logger>

to avoid my own log file being cluttered by org.apache.commons messages (no clue why they end up in my own com.acme.osb logger).


Appendix: custom xpath definitions:

<?xml version="1.0" encoding="UTF-8"?>
<xpf:xpathFunctions xmlns:xpf="http://www.bea.com/wli/sb/xpath/config">
    <xpf:category id="logMessage">
        <xpf:function>
            <xpf:name>logMessage</xpf:name>
            <xpf:comment>logMessage</xpf:comment>
            <xpf:namespaceURI>http://www.bea.com/xquery/xquery-functions</xpf:namespaceURI>
            <xpf:className>com.acme.osb.OSBLogger</xpf:className>
            <xpf:method>java.lang.String logMessage(java.lang.String)</xpf:method>
            <xpf:isDeterministic>true</xpf:isDeterministic>
            <xpf:scope>Pipeline</xpf:scope>
            <xpf:scope>SplitJoin</xpf:scope>
        </xpf:function>
        <xpf:function>
            <xpf:name>reloadConfiguration</xpf:name>
            <xpf:comment>reloadConfiguration</xpf:comment>
            <xpf:namespaceURI>http://www.bea.com/xquery/xquery-functions</xpf:namespaceURI>
            <xpf:className>com.acme.osb.OSBLogger</xpf:className>
            <xpf:method>java.lang.String reloadConfiguration(java.lang.String)</xpf:method>
            <xpf:isDeterministic>true</xpf:isDeterministic>
            <xpf:scope>Pipeline</xpf:scope>
            <xpf:scope>SplitJoin</xpf:scope>
        </xpf:function>
    </xpf:category>
</xpf:xpathFunctions>




No comments: