Tuesday, September 27, 2011

Oracle SOA Suite 11g - How to use its native logging mechanism

(From http://mazanatti.info/index.php?/archives/48-SOA-Suite-11g-how-to-use-its-native-logging-mechanism.html)

If you ever wondered how to generate log messages inside SOA Suite's logging files (diagnostics.log), here are the directions.

From JDeveloper, with a BPEL flow opened, do this:

■Insert some imports at the top of the source (first ones inside process tag):
 <bpelx:exec import="java.util.logging.Logger" />  
 <bpelx:exec import="java.util.logging.Level" />  
 <bpelx:exec import="oracle.fabric.logging.LogFormatter" /> 

■Insert a Java Embedding action with the following code:
 Logger logger = Logger.getLogger("oracle.soa.Logger");  
 logger.log(Level.INFO, "some message"); 

That's it, basically. The trick is to configure the logger instance using SOA Fabric's LogFormatter.

To avoid doing these steps every time you need to log something, you can use a class.
■Create a new Java Class inside your composite project, with the code below:
 package info.mazanatti.soa;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 import oracle.fabric.logging.LogFormatter;  
 public class CustomLogger() {  
    private static final Logger logger = Logger.getLogger("oracle.soa.Logger");  
    static {  
    public static final void log(String message) {  
       logger.log(Level.INFO, message);  

■Call the function from your Java Embedding actions:
info.mazanatti.soa.CustomLogger.log("some message"); 

After building, deploying and running your process, this is what you're going to see from Enterprise Manager's Log page.

Here's the JDeveloper project using both methods: LoggingBPEL.zip. This code has "info.mazanatti.Logger" instead of "oracle.soa.Logger" as showed above. If you want to know why, keep reading this post :-)

There's one detail you may have seen that deserves some explanation: I passed a specific package and class (oracle.soa.Logger) in order to retrieve a logger instance. I did this 'cause this package is configured by default when installing the product, and this releases us of some configuration.

But, if you want to use a specific log level for your messages without messing with anything else, the best way to do so is to define a new package and configure it accordingly. Here's how.

  • Define the package you want to use. I'm going with "info.mazanatti".
  • Locate the file logging.xml of your server and open it. Inside the domain folder, navigate to /config/fmwconfig/servers/server_name.

    Warning: you have to configure the logging.xml file of each SOA instance (server). Not the most cluster-friendly procedure, but that's the way it is.
  • Include a XML block like this, specifying your package and the lowest log level you want:
    <logger name="info.mazanatti" level="WARNING:1">  
        <handler name="odl-handler" />  
  • Start your domain
  • Meanwhile, go back to JDeveloper and change the getLogger parameter (hint: the class doesn't need to exist, only the package name is actually used):
    Logger logger = Logger.getLogger("info.mazanatti.Logger");


    logger.log(Level.INFO, "some message");
  • Deploy the process to the server, and it's done.
The odl-handler mentioned above is the one that writes to diagnostics.log. There are other handlers, but I'm not getting into them - if you want, take a look at the log_handlers block inside logging.xml to figure out what the other handlers do ;-)

And here's the list of log levels and its relation to java.util.logging.Level, in case you want to use values other than Level.INFO:

logging.xml java.util.logging.Level
Since we're here, a little extra bit of information: to view the logging configuration, you must open Enterprise Manager and navigate to "Log Configuration", as showed below.

Then, a page with data from the logging.xml file will be presented.

If you're not seeing your package listed, don't worry. It will not show up here, ever. Can't say why, but know that the configuration is saved and effective. In case you want to change the log level of your custom package, you don't have to edit the xml files again.
  • At this page, select the option "Loggers with Persistent Log Level State" from the "View" dropdown
  • At the bottom of the new page, a section called "Specify Loggers" will show up. Type your package name and select the new Level, then click "Apply" at the top of the page
Take a look at your logging.xml file, the new value will be there.
Again, here's the source code: LoggingBPEL.zip.

No comments: