-
Notifications
You must be signed in to change notification settings - Fork 36
System.out & System.err Stream Redirection
August Detlefsen edited this page Jun 27, 2017
·
2 revisions
The stream redirection feature is useful when a legacy application logs to System.out
and System.err
streams directly. Using the stream redirection feature these messages will be captured and directed to a SLF4J logger. This is not as useful as native logging to an SLF4J logger but it is still better than logging to the system stream classes. A quick summary is as follows.
Benefits of stream redirection:
- Messages logged to system streams will be handled by SLF4J logging framework
- Legacy application can leverage appenders and other powerful features with no code changes
- Applications using log4j or logback will realize benefit if any messages are mistakenly logged to system stream classes since these would now be captured in application logs
Limitation:
- All redirected log messages from system streams are logged in same logger namespace (e.g., package.classname). The impact is that it's difficult to isolate some of these log messages from others.
While stream redirection is a poor substitute for native SLF4J logging, it is quick and easy to do, and adds some value over not doing it. The bottom line is that some improvement in logging is better than no improvement at all and the effort to implement stream redirection is minimal. Consider the following code fragment,
...
System.out.println("Messages not going to SLF4j. So sad.");
SecurityUtil.bindSystemStreamsToSLF4J();
System.out.println("Whoot, now I'm printing to SLF4J.");
System.out.flush();
SecurityUtil.unbindSystemStreams();
System.out.println("Stream restored. Messages not going to SLF4J. Sad yet again.");
System.out.flush();
...