Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who should log an error/exception

I am trying to figure out the best practices when loggin exceptions.

So far, I am logging every time I catch an exception. But when a lower lever class catches an exception (say, from the database layer), and wraps it in our own application exception - should I also log the original exception there, or should I just let the upper lever class log all the details?
And what about locations where my lower level class decides to throw an exception because of bad input parameters? Should it log the exception there also, or, once again, just let the catching code log it?

like image 257
Noam Gal Avatar asked Aug 13 '09 12:08

Noam Gal


2 Answers

Mainly you should avoid logging it in both a lower-level catch and a higher-level catch, as this bloats the log with redundant information (not to mention takes up additional IO resources to write to the log).

If you are looking for general best practice information on exception handling, this link is handy.

like image 91
Nelson Avatar answered Sep 22 '22 07:09

Nelson


You can get away with logging only once at the very top level of your app, as long as your logging code (a) logs the stack trace of an exception, and (b) logs the entire chain of inner exceptions as well.

The Microsoft Exception Handling Application Block takes care of both of those things for you. I guess other logging frameworks would do the same.

like image 34
Christian Hayter Avatar answered Sep 23 '22 07:09

Christian Hayter