views:

352

answers:

3

I am writing an error logging set of classes which will log to file, event log etc. What exception handling should be performed within these classes? For instance, say I have a LogError method, which is called from exception handlers, and writes to file. What is considered the best thing to do, should an error occur? Obviously, I should make these methods as fail safe as possible, but problems can always occur.

+1  A: 

Generally I output to stderr as much information as possible in that case, often both the error/exception in the logging code, and the original log/error/exception. That way there's a chance of reproducing the problem or understanding it.

If writing to stderr fails then it's time to give up - either ignore it, to terminate the application entirely.

Douglas Leeder
+1  A: 

Why don't you use an existing logging mechanism such as log4j/log4net/log4php/log4*? These tools probably have those details sorted out.

As a side note, if you run your code inside a container (eg: tomcat) even if you throw an exception inside the exception handler the container would catch it and show it. Like Douglas Leeder said, you can catch all exceptions inside the handler and throw it to the syserr.

Miguel Ping
+1 don't re-invent the wheel.
Gerrie Schenck
Yes, but the log4net wheel has rather more bells and whistles attached to it than we need. I've written a number of error loggers in my time, and they are always simple and do what is required of them. It's not difficult.
darasd
A: 

It depends what the logging is for, if it's debug logging I'll swallow the exception and continue because I never want the application to fail in production due to debugging aids, on the other hand if I'm logging to the audit log of a banking application I guess the customer is going to be upset if the application continue to work without auditing.

Nir
I'm error logging.
darasd