cross-platform logging.config: how to set cross platform log (handlers) file location?

Peter Otten __peter__ at web.de
Tue Aug 9 09:30:52 EDT 2016


berteh at gmail.com wrote:

> Hello.
> 
> My python script should run on Linux, Win and MacOS, but I can't find a
> way to have logging.conf configuration that works out of the box accross
> all these OSes.
> 
> I don't want my users to have to edit configuration files manually.
> 
> 
> If I use a "linux-like" handler configuration such as below it fails on
> Windows when the script is run from a directory from which the user has no
> write access (ie any subfolder or C:\Program Files, that is the default
> install location)
> 
> [handler_file]
> class=handlers.RotatingFileHandler
> args=('scribusGenerator.log','a','maxBytes=1000','backupCount=1')
> 
> And if I use a "windows-like" file location it fails (of course) in Linux
> and MacOSX
> 
> [handler_file]
> class=handlers.RotatingFileHandler
> args=('D:\scribusGenerator.log','a','maxBytes=1000','backupCount=1')
> 
> 
> One option would be to set the location dynamically in the system temp
> directory (from tempfile.gettempdir())... but I can't find a way to change
> the location of the handler loaded via config module.
> 
> Any piece of advice on how to achieve cross-platform logging that does not
> require a modification by the user?

args is passed to eval(), so

args=("D:\scribusGenerator.log" if sys.platform == "win32" else "scribusGenerator.log", ...)

should work.




More information about the Python-list mailing list