Configuring custom handlers

This recipe explains how to configure different logging handlers in the Oracle SOA Suite. Although many of the logging handlers already exist, occasionally we may need some special functionality, such as the ability to log into databases or logging via web services. The number of log handler classes in the Oracle SOA Suite is quite limited. Actually, if you check the logging.xml file, you'll only find two types: oracle.core.ojdl.weblogic.DomainLogHandler and oracle.core.ojdl.logging.ODLHandlerFactory. For this recipe, we will define a custom handler and use it in our configuration. The custom handler will enable each instance of the BPEL process to log into its own logfile.

How to do it…

The following steps describe the actions required to create and configure a custom log handler:

  1. Start by creating an empty Java project in JDeveloper and name it LoggingHandler.
  2. In the project, create a Java class (CustomLogger.java) in the logging package as shown in the following code:
    package logging;
    public class CustomLogger extends Handler {
  3. This new class extends the Java Logging class, java.util.logging.Handler, which presents the default template class to design the new log handlers. CustomLogger will create a logfile in the Java temp directory and name it with your computer name and append the extension log to it.
  4. To proceed, we define two output streams. They are used to direct flow of the logs to the output file as shown in the following code:
    FileOutputStream fileOutputStream;
    PrintWriter printWriter;
  5. Continue with the definition of the constructor. We first concatenate the log filename from the various sources as shown in the following code:
    InetAddress iAddress = InetAddress.getLocalHost();
    String hostName = iAddress.getHostName();
    String tempDir= System.getProperty("java.io.tmpdir");
    String fileLoc= tempDir + hostName + ".log";
    And finally create concrete output streams:
    fileOutputStream = new FileOutputStream(fileLoc);
    printWriter = new PrintWriter(fileOutputStream);
  6. To create the operable log handler, we have to override three methods. The first one is the publish method, which is used to write data to the file as shown in the following code:
    public void publish(LogRecord record) {
      if (!isLoggable(record))
        return;
        printWriter.println (getFormatter().format(record));
    }
  7. We also override the flush method because we need to flush the content to the output stream as shown in the following code:
    public void flush() {
      printWriter.flush();
    }
  8. We also override the close method as shown in the following code:
    public void close() {
      printWriter.close();
    }
  9. Now that we have the custom logger class written, we prepare the package for deployment. At the project root, right-click and select Project Properties…. In the tree, select Deployment and then click on the New… button as shown in the following screenshot:
    How to do it…
  10. The Create Deployment Profile dialog shows up where we name our JAR file. Click on the OK button as shown in the following screenshot:
    How to do it…
  11. We get the Edit JAR Deployment Profile Properties dialog and we can simply confirm it by clicking on the OK button. Close the Project Properties… dialog by clicking on the OK button.
  12. We'll create a jar file now. On the project root icon, right-click and select deploy and then select the deployment profile you just prepared for the JAR creation. The wizard opens but we skip the steps and click on the Finish button. The created JAR file is waiting to be picked at the deploy directory of the project.
  13. Copy the JAR file to the Oracle SOA Suite server shared library location that is located at:
    ${domain.home}/lib
  14. Now we need to configure the custom handler. Unfortunately, this step needs to be performed manually, because the Oracle Enterprise Manager Console does not show the newly added custom handler. So, let's locate the logging.xml file that holds the logging configuration at the following path:
    ${domain.home}configfmwconfigserversAdminServerlogging.xml
  15. Open the logging.xml file and define a new log handler inside it as shown in the following code:
    <log_handler name='custom-java-handler' class='logging.CustomLogger' level='TRACE:32' formatter='java.util.logging.SimpleFormatter'/>

    We can see that the handler as well as the formatter belongs to the Java Logging utility.

  16. Add the log handler to the logger's configuration. Note that the name of the handler must be identical to the one in the <log_handler> element as shown in the following code:
    <logger name='' level='WARNING:1'>
      <handler name='odl-handler'/>
      <handler name='wls-domain'/>
      <handler name='console-handler'/>
      <handler name='custom-java-handler'/>
    </logger>
  17. The last action we need to perform is to restart the Oracle SOA Suite in order to pick up the JAR file we generated in the previous steps. If we initiate the instance of the BPEL process now, we see that some lines of logging appear in the logfile.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset