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.
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.
In order to redirect the System.out
and System.err
streams, we create two additional classes in the project:
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.
Redirector
singleton class used as a crossover between the redirected and non-redirected logging stream.private static PrintStream origOut; private static PrintStream origErr;
Redirector
class are activate()
and deactivate()
, which are used as the activator and deactivator of the redirection streams respectively.activate()
method:public static void activate() { origOut= System.out; System.setOut(new LogStream(System.out)); origErr= System.err; System.setErr(new LogStream(System.err)); }
deactivate()
method:public static void deactivate() { System.setOut(origOut); System.setErr(origErr); }
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.