logging.config.fileConfig FileHandler configure to write to APP_DATA

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Apr 17 09:13:24 EDT 2012


Jeffrey Britton wrote:
> I figured out what I was after.
>
> logfn = os.path.join(os.environ['APPDATA'], directory, filename)
> ensure_path(logfn)
> logging.config.fileConfig("logging.conf", defaults={'logfn': logfn})
>
> In the config file use:
> [handler_fileHandler]
> class=FileHandler
> level=DEBUG
> formatter=simpleFormatter
> args=(r'%(logfn)s', 'w')
>
>
>
>   
Hi,

An alternative is to subclass FileHandler with a handler customized for 
your app.

class AppFileHandler(FileHandler):
    def __init__(filename):
       if not os.path.isabs(filename):
          filename = os.path.join(os.environ['APPDATA'], 'whateverdir', 
filename)
       FileHandler.__init__(self, filename, 'w')

[handler_appFileHandler]
class=AppFileHandler
level=DEBUG
formatter=simpleFormatter
args=('foo.log')


It removes the cryptic args=(r'%(logfn)s', 'w') which may puzzle users.


I didn't test the code, it could contain some errors, but you got the idea.

JM



More information about the Python-list mailing list