• Post Reply Bookmark Topic Watch Topic
  • New Topic

redirecting stdout and stderr to a logback appender  RSS feed

 
Jeevan Sunkersett
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Mine is a j2EE, application working in a j2EE server.

For tracing execution I use logback/ sl4j instead of System.out.println() ... so that execution can be diagnosed by reading the log file

However, in my application I use a 3rd party jar, that internally uses System.out.println(), even exceptions were being printed using e.printStackTrace().

How can I redirect the stdout and stderr of this 3rd party jar, to the RollingFileAppender defined use in my SL4J.

I cannot update/ edit the 3rd party jar.

thanks
Jeevan
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can change what System.out and System.err actually refer to by using System.setOut and System.setErr. However, you would need to find a good alternative for the PrintStream for these methods. I'm not sure it will be easy to get hold of the RollingFileAppender's internal streams. Furthermore, you would need to reset System.out and System.err every time the appender performs a "roll", because it will get different streams (closing the old ones, opening new ones).
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was keeping mum until someone suggested something simple and obvious. But that's about as close as I could come myself.

you should (theoretically) be able to replace the System.out and System.err values with alternative OutputStreams that funnel themselves into code that writes the text lines into a log via whatever logging interface (juli, log4j, whatever) you like.

Regardless, a library in this day and age that is so brutal isn't well-designed. I'd recommend looking for a more civilized equivalent.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using SLF4J, then there are ways to redirect stdout and stderr to the logger, see this in the SLF4J manual: System Out and Err redirected to SLF4J
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be careful though, some SLF4J implementations may redirect logging to System.out, so you can end up with endless loops.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!