Properly handling IOError thrown by logging.config.fileConfig?

Assuming it only needs to be done once at the beginning of your app's execution, I would just os.makedirs() all the needed directories without checking for their existence first or even waiting for the logging module to raise an error. If you then you get an error trying to start a logger, you can just handle it the way you likely already did: print an error, disable the logger. You went above and beyond just by trying to create the directory.

If the user gave you bogus information, you're no worse off than you are now, and you're better in the vast majority of cases.

What concerns me with this approach is that I would need to parse, then iterate through the logging configuration file, check the args variable if it is a file handler, and take action. While this is not a challenge, it seems like a lengthy solution to a problem. Why should I need to do this?

Why doesn't the logging module do this for me? – Locker537 2 days ago Hrm, yeah, I didn't notice at first that the filenames you were passing to the log file were those of logging config files rather than the log files. It looks like Vinay has a suggestion for doing this with a custom FileHandler which is probably a better way.

– kindall 2 days ago While Vinay's solution is certainly a good idea, do you feel that it is overkill for this problem? – Locker537 yesterday No, it seems to me to be the way such an issue was intended to be handled. The file handler appears to be the class that's raising the exceptions you're catching in your example code, so if you could substitute your own file handler that created the directories as needed, that seems pretty surgical to me.

– kindall yesterday Surgical, yes, but think of a novice developer simply trying to use the logging module. That's a bit involved for a novice. Albiet, it will probably be the way I go.

– Locker537 yesterday.

This is not something specific to the logging module: in general, Python code does not automatically create intermediate directories for you automatically; you need to do this explicitly using os.makedirs(), typically like this: if not os.path. Exists(dirname): os. Makedirs(dirname) You can replace the standard FileHandler provided by logging with a subclass which does the checks you need and, when necessary, creates the directory for the logging file using os.makedirs().

Then you can specify this handler in the config file instead of the standard handler.

While I agree the design is sound, it seems like a complicated way to handle an exception (or set of). – Locker537 yesterday.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions