I'm using the standard python (2.5.2) logging module, specifically the RotatingFileHandler, on a linux system. My application supports both a command-line interface and a web-service interface. I would like to have both write to the same log file. However, when the log file gets rotated, the new file has 644 permissions and is owned by the web server user which prevents the command-line user from writing to it. Can I specify that new log files should be group-writable in the logging configuration or during logging initialization?
I have looked into the 'mode' setting (r/w/a), but it doesn't seem to support any file permissions.
James Gardner has written a handler that only rotates files, not creating or deleting them: http://packages.python.org/logrotate/index.html
Here is a slightly better solution. this overrides the _open method that is used. setting the umask before creating then returning it back to what it was.
I resorted to scanning the logging.handlers module and was unable to see any way to specify a different file permissions mode. So, I have a solution now based on extending the RotatingFileHandler as a custom handler. It was fairly painless, once I found some nice references to creating one. The code for the custom handler is below.
I also discovered that to reference the custom handler from a logging config file, I had to bind my module to the logging namespace. Simple to do, but annoying.
References I found useful: binding custom handlers and creating custom handlers
Here's a complete solution for Django based on rob's solution. in
my_module
:The
class=
that happens during the logging configuration is evaluated in the namespace of the logging module, and by default this does not have a binding to handlers. So we have to put it in explicitly before we can extend it. See this SO articleIt's this magical incantation that took me forever to find - I couldn't believe it did anything! Finally, Jon's class will load without errors.
To use this for Django add the following this in the settings file
And then in
LOGGING['handlers']['file']
you have