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.
basicConfig(filename=log_file_name) where log_file_name is the name of the file you want messages written to (note that you have to do this before anything else in logging is called at all), then all messages logged to all loggers (unless some further reconfiguration happens later) will be written there.
A Python library for managing logging directories. Source. PyPI.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With