Reassign stdout and stderr to BW logger

There are instances when you are using a third party Java class and you want to debug certain errors, but you couldn't get the error stack traces in the logs particularly in the application logs which you can also view via TIBCO Administrator (assuming you have no server-level access). There's no way you can change the Java class to add a custom log4j logger such as "bw.logger" in order to write into the BW log file. You know for certain that the Java class writes debug logs into the standard output and error stream (e.g. System.out and System.err).

You can reassign standard and error output stream and into the BW log file by writing a custom Java code. Since these are static methods and a service instance runs in its own JVM, you only have to run this code once as part of a process starter (process definition with onStartup).

Create a static Logger object (class org.apache.log4j.Logger).

private static final Logger logger = Logger.getLogger("bw.logger");

Create a static method that overrides println and print methods. Checked exceptions are usually written into the standard error stream using printStackTrace method of the Throwable class, which goes into the print method of System.err.

private static PrintStream createLoggingProxy(final PrintStream realPrintStream) {
            return new PrintStream(realPrintStream) {
                    public void println(final String string) {
                        realPrintStream.println(string);
                        logger.info(string);
                    }
            public void print(final String string) {
                        realPrintStream.print(string);
                if (string.contains("Caused by:")) {
                            logger.warn(string);
                }
                    }
            };
        }

Finally, use System.setOut and System.setErr to do the trick!

System.setOut(createLoggingProxy(System.out));
System.setErr(createLoggingProxy(System.err));

Comments

  1. I am trying to do this to capture SSL debug info after setting bw.plugin.http.server.debug=true and
    java.property.javax.net.debug=ssl in the tra file.

    Unfortunately the console output for SSL debug is not written to the Tibco log using this method. Any idea why ?

    ReplyDelete
  2. Did you create a process starter that runs a Java Code activity as shown above?

    ReplyDelete
  3. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    Thank you for sharing wonderful information with us to get some idea about that content. It is very useful.Thanks for sharing.
    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    oracle online training

    hadoop training in chennai

    hadoop training in bangalore



    ReplyDelete

Post a Comment

Popular posts from this blog

XML Schema and JSON Schema Validation in Mule 4

Parsing a JSON String and Converting it to XML in TIBCO

Using XML To Java in TIBCO BW