开发者

Handling exceptions that are caused by logging

We all know that logging is very important, and that there are a multitude of potential places to log to. (e.g. a file, a database, the event log, ...)

However, what do you do when the logging itself throws an exception? If we try to log to a file, but don't have permissions, or the file is locked, we can log to the event log. I don't know how it would happen, but I assume that there is some scenario that could c开发者_如何学JAVAause logging to the event log to also fail. How would you handle exceptions that occur while logging to ensure that it is reported somewhere?

What methods of logging are least likely to fail? What methods of logging are most likely to fail?

My current scheme is for all logging to go to a file, with a little bit also going to the event log. If logging to the file fails, I would log that to the event log. I don't really have a good plan for the event log failing.


I'd say you're going too deep. Logging frameworks should take care of themselves. That means: no exceptions, unless something really horrid is going on behind the scenes.

File locking exceptions should never appear. If they do, your logging framework has a flaw. That means you're using a wrong framework, since that would be a very fundamental flaw.

Secondly, file permissions. It is YOU who decides where logged files will appear. If you don't take into account file and directory permissions, it is your fault. You must make sure your logger can log where you tell it to log.

Bottom line: log to files. This is the most convenient way. Also, the fastest way. Logging to a database can always fail. If the framework is robust enough, it won't throw any exceptions. You must ensure that file permissions are set up properly. And that's all there is to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜