logging.config.fileConfig FileHandler configure to write to APP_DATA

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Apr 18 05:36:36 EDT 2012


Jeffrey Britton wrote:
>> 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')
>>
>>
>> JM
>>     
>
> Thanks,
>
> This is great, but how does parser get the definition for AppFileHandler?
>
> This could potentially solve my next problem.
> I am using the logging listener and sending new log configurations to
> it via a socket.
> I don't know how to pass a dictionary to the parser in this scenario.
> So, if AppFileHandler can get me around this problem that would be great.
>   
I don't really know I'm not using config files. By looking at 
logging/config.py, the class is given that way:
klass = 'AppFileHandler'
klass = eval(klass, vars(logging))

So by simply writing
logging.AppFileHandler = AppFileHandler
you would "register" you custom class as a logging attriute and should 
be able to use it in your config files.

JM



More information about the Python-list mailing list