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.
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().
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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 ]