• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

redirecting stdout and stderr to a logback appender

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Saloon Keeper
Posts: 25483
180
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 13894
314
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic