This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Intercept And Formatting System.err  RSS feed

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Does anyone knows how to format the contents of System.err?
The System.err.println and printStackTrace doesn't have
a timestamp.

I intend to redirect the System.err to a log file with timestamp
for the entries. The redirection is fine but I didn't managed
to add in the timestamp.

Any idea?


Please advise.

Thanks in advance.
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using System.err.println, maybe you should use a logger like log4j to print to a file with the correct format (including a timestamp)
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

Well in production code we never use System.out and System.err for logging purposes. However if you have bae code with SYSOUTs then you can use redirection for output stream.

A good place to refer to this would be :
Redirecting SYSOUTs

 
James Gordon
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm currently using log4j.
All the intended logging are through log4j.

But how about those which are not caught by
our application? It'll got to System.err right?

I redirected these uncaught errors to a log file.
But having the log file is not sufficient
unless the entries have timestamp. Else, we won't
be able to identify when the error was recorded.
And this information is vital in production environment.


Please comment.

Thanks.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YOu can do System.setOut( someStream ). I suppose you could override println() to write a timestamp in front of every line. Not sure what you'd do about print() though. This sounds way too familiar. I might have done it once and left the ancestor write().
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's probably better to figure out where those uncaught exceptions are coming from, and find ways to catch them. You don't need to know what the exact causes are - you just need to find places high up in your call stack where you can insert a few try/catch blocks that will ensure better logging. E.g.

Normally I would not encourage catching something as general as Throwable, but for the situation you describe it makes perfect sense. You want more sophisticated erro handling elsewhere in the code, but at this point, you just want to make sure you catch anything that has otherwise slipped through the creacks, and log it.

Of course, if you have other threads, or if you're running in some sort of framework or container that is calling your code with something other than a main() method, you may have to put this sort of code somewhere else. Maybe in a run() method, or a servlet's doGet() or doPost() method, or whatever method there is that is farthest down the call stack in code that you acually have access to.

Alternately, you might also use Thread.setUncaughtExceptionHandler(), and Thread.setDefaultUncaughtExceptionHandler(). If you're using JDK 1.4 or earlier, an equivalent method is in ThreadGroup instead. Hm, maybe I should have listed this first, as it may well be easier than the other stuff I typed.
[ September 19, 2006: Message edited by: Jim Yingst ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WARNING : This is container specific

If you are using Tomcat, there's an option to redirect System.out and System.err to a file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!