[slf4j-user] Redirecting Outputstream to the logger

Ceki Gulcu ceki at qos.ch
Wed Mar 25 15:48:40 CET 2009


Hello Erik,

Thank you for posting an initial implementiation. However, the code is a 
contribution from Jim Moore to the log4j project. (You can find in log4j SVN 
under the contrib directory). Thus, it implicitly carries the the Apache 
Software License.

Also note that Jim's code does not deal with exceptions. Ideally, one would 
capture exceptions as well. If you look at the printStackTrace method in the 
Throwable class, you will see that the method

1) invokes s.println(this), where s is System.err,
2) invokes  s.println("\tat " + trace[i]) where trace[i] is the i th stack frame
3) If there are nested exceptions, invokes s.println("Caused by: " + nested)
4) invokes s.println("\tat " + trace[i]) where trace[i] is the i th stack frame 
in the nested exception
5)  invokes s.println("\t... " + framesInCommon + " more") if there are any 
common stack frames, which there always are for nested exceptions
6) continue with step 3 if the nested exception has itself nested exceptions

Step 1 can serve as a starting point for entering exception capture mode. Any 
string not matching the patterns in steps 2-8 can serve as a point for exiting 
capture mode. Admittedly, it's kind of a messy/tricky.

Binary instrumentation of the Throwable.printStackTrace() method with a java 
agent could constitute a much more elegant solution.

Erik van Oosten wrote:
> Hi,
> 
> We are currently using the implementation attached below. It was found 
> on the web for free use. It bears no copyright statement, only the 
> author's name so I guess it is good for open sourcing properly.
> 
> The implementation is written for log4j, adapting it for slf4j should be 
> trivial.
> 
> Regards,
>    Erik.
> 
> 
> 
> Papick Garcia Taboada wrote:
>> Hi,
>>
>> I have an API that outputs some usefull information to an OutputStream.
>> I would like to pipe this to slf4j.
>>
>> Is there a nice wrapper for that?
>>
>> brgds,
>>
>> Papick
-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch



More information about the slf4j-user mailing list