0

While I use logging to record exceptions, it occurred to me the methods of the logging library itself can throw exceptions.

For example, if a "bad" path is set for the log file, like

import logging
logging.basicConfig(filename='/bad/path/foo.log')

a FileNotFoundError will be thrown.

Suppose my goal of handling these logging exceptions is to keep the program running (and not exit, which the program would otherwise do). The first thing that comes to mind is

try:
    logging.basicConfig(filename='/bad/path/foo.log')
except Exception:
    pass

But this is considered by some to be an antipattern. It's also quite ugly to wrap every logging.error with try and except blocks.

What's a good pattern to handle exceptions thrown by logging methods like basicConfig(), and possibly by debug(), error(), etc.?

8
  • The example you have given is one about a wrongly configured logging module. Why not configure it properly? Commented Jun 12, 2020 at 4:38
  • @rdas Just like runtime bugs, getting configurations right is not always as trivial as this example. This becomes more challenging if there are multiple deployment environments (e.g. staging, production, colos, Docker, AWS, etc.). Was this your line of questioning, or did you have something else in mind? Commented Jun 12, 2020 at 21:09
  • 1
    Configurations issues should be fixed with proper configuration - not code. IMO. Commented Jun 13, 2020 at 7:44
  • If there's a configuration issue, e.g. I've made a mistake, I want to know about it, not have it silently ignored by the application. Commented Jun 13, 2020 at 10:35
  • 1
    No I think you missed my point entirely. If you think that your logger is "every little thing" then yeah you can wrap your logger in an object & use that to handle the exceptions thrown. Personally I wouldn't do that. Just configure your logger properly. If this same issue comes up in some other place (instead of the logger) - reevaluate & take it on a case-by-case-basis Commented Jun 16, 2020 at 5:00

1 Answer 1

1

Unless you re-initialise your logger mid-way through your code, why not just check whether the file exists during logger initialisation:

import os

if os.path.isfile(filename):

    # Initialise the logger 

This should work normally, unless of course some part of the later code will attemp to delete the file, but I hope that it's not the case.

Sign up to request clarification or add additional context in comments.

3 Comments

This idea could be made to work for that particular error I think - the next step would be to think about what to do if the file doesn't exist. Should some other logger be configured, so the logging methods sprinkled throughout the codebase have an expected behavior?
Also, is it not possible for other Exceptions to be thrown by logging as well?
@flow2k, I guess you need to check out doc for that. It's quite possible there will be quite a few possible exceptions thrown by logging module. You can also use assert in the beginning instead of if clause. That way if the file doesn't exist the program will throw AssertionError at the very start and not waste time running through other code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.