Redirecting System.out and System.err files

In some cases, we may want to integrate into our BPEL processes other components or applications that print their log information either to standard output, standard error, or both. This recipe will show you how to redirect those outputs to the logfile in order to have logging information in one place.

Getting ready…

To complete the recipe, we will amend the BPEL process from the Logging exceptions recipe. Since the main flow of the BPEL process already throws an exception, we will continue explaining this recipe at the fault handler branch of the BPEL process.

How to do it…

In order to redirect the System.out and System.err streams, we create two additional classes in the project:

  1. The LogStream class extends the Java PrintStream class. Although there are a number of constructor methods and a number of overridden methods, there is only one method we will describe here, and that is println of type string, as shown in the following code:
    @Override
    public void println(String x) {
        StackTraceElement caller = Thread.currentThread().getStackTrace()[2];
        LogTheBpelExt.log( caller.getClassName() + "  " + caller.getMethodName() + "  " + caller.getLineNumber() + ": " + x);
    }

    In the preceding code, we redirect the stream from calling its super method to call of the log method in our custom logger class. Besides that, we also decode the string with useful information about the call stack.

  2. We also implement the Redirector singleton class used as a crossover between the redirected and non-redirected logging stream.
  3. First, we declare two stream variables that we use as a backup of the original streams as shown in the following code:
    private static PrintStream origOut;
    private static PrintStream origErr;
  4. The most important methods in the Redirector class are activate() and deactivate(), which are used as the activator and deactivator of the redirection streams respectively.
  5. The following is the code for the activate() method:
    public static void activate() {
      origOut= System.out;
      System.setOut(new LogStream(System.out));
      origErr= System.err;
      System.setErr(new LogStream(System.err));
    }
  6. The following is the code for the deactivate() method:
    public static void deactivate() {
      System.setOut(origOut);
      System.setErr(origErr);
    }
  7. Now it is time to use the two new classes in the BPEL process. We add the Java Embedding activity into the BPEL process. Insert the following code into the activity:
    System.out.println("Some output from out - no redirect");
    System.err.println("Some output from err - no redirect");
    org.packt.redirect.Redirector.getinstance().activate();
    System.out.println("Some output from out - redirect");
    System.err.println("Some output from err - redirect");
    org.packt.redirect.Redirector.getinstance().deactivate();

From the preceding code, we can predict that the first two lines will be shown on the console output. Then, we activate the redirection of the output streams. The following two lines should appear in the custom logfile. And finally, we deactivate the redirection. The redirection is taking effect on the BPEL process instance.

Indeed, if we deploy the BPEL process and observe the execution of the instance, we first see the following output in the console:

Some output from out - no redirect
Some output from err - no redirect

Further, we also see the following two lines in the custom logfile (AdminServer-custompackt.log):

<6.5.2013 23:26:34 CEST> <Warning> <org.packt.log.Logger> <BEA-000000> <orabpel.customlog.ExecLetBxExe2  execute  87: Some output from out - redirect>
<6.5.2013 23:26:34 CEST> <Warning> <org.packt.log.Logger> <BEA-000000> <orabpel.customlog.ExecLetBxExe2  execute  88: Some output from err - redirect>

We can conclude that the redirection succeeded as we saw in the console and in the custom logfile.

..................Content has been hidden....................

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