logging.config.fileConfig FileHandler configure to write to APP_DATA

Jeffrey Britton jeffb0098 at gmail.com
Wed Apr 18 14:12:30 EDT 2012


I was sent via email an alternative solution which is working even
better for me.

In Python code use:
class AppFileHandler(logging.FileHandler):
    def __init__(self, *args):
        filename, mode = args
        if not os.path.isabs(filename):
            filename = os.path.join(os.environ['APPDATA'],
someDirectory, filename)
        logging.FileHandler.__init__(self, filename, mode)

logging.AppFileHandler = AppFileHandler
logging.config.fileConfig("logging.conf")


In the logging configuration file use:
[handler_fileHandler]
class=AppFileHandler
level=DEBUG
formatter=simpleFormatter
args=('vector.log', 'w')


This solution is better, because I am also using
logging.config.listen() which allows a new config file to be sent via
a socket to reconfigure a running process's logging.  When calling the
parser directly I could pass a dictionary with the log filename, but
when invoking via the socket, the parser is called indirectly and I
could not pass it the dictionary.  The AppFileHandler solution works
via the socket listener also.



More information about the Python-list mailing list