python logging: how to ensure logfile directory is created?

Question:

I would like to use python’s logging framework in my application, and I’d like to allow the end user of my application to specify the log file. (Via the Python logging framework’s configuration mechanisms which in my case is a section of a YAML file that the end user can edit to specify how logging behaves.)

Is there a way to get the logging framework to ensure that a directory exists by creating it? Because the exact path to the logging filename is embedded in the configuration information specified by the end user, it is nontrivial for me as the application writer to parse this information to determine which directory should be created.

If the end user specifies “foo/bar/baz.log”, I would like to make sure that the foo/bar directory is created.

Note: This is the Python equivalent of this SO question about Java logging.

Asked By: Jason S

||

Answers:

Subclass FileHandler (or whatever handler you are using) to call mkdir_p during initialization:

import logging
import os
import errno

def mkdir_p(path):
    """http://stackoverflow.com/a/600612/190597 (tzot)"""
    try:
        os.makedirs(path, exist_ok=True)  # Python>3.2
    except TypeError:
        try:
            os.makedirs(path)
        except OSError as exc: # Python >2.5
            if exc.errno == errno.EEXIST and os.path.isdir(path):
                pass
            else: raise

class MakeFileHandler(logging.FileHandler):
    def __init__(self, filename, mode='a', encoding=None, delay=0):            
        mkdir_p(os.path.dirname(filename))
        logging.FileHandler.__init__(self, filename, mode, encoding, delay)
Answered By: unutbu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.