Does python logging.handlers.RotatingFileHandler allow creation of a group writable log file?

前端 未结 7 656
太阳男子
太阳男子 2021-01-31 08:40

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 interfa

7条回答
  •  误落风尘
    2021-01-31 09:05

    Here's a complete solution for Django based on rob's solution. in my_module :

    import logging
    import logging.handlers
    import os
    

    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 article

    logging.handlers = logging.handlers
    

    It'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.

    class GroupWriteRotatingFileHandler(logging.handlers.RotatingFileHandler):    
        def _open(self):
            prevumask = os.umask(0o002)
            rtv = logging.handlers.RotatingFileHandler._open(self)
            os.umask(prevumask)
            return rtv
    

    To use this for Django add the following this in the settings file

    from my_module import GroupWriteRotatingFileHandler
    logging.handlers.GroupWriteRotatingFileHandler = GroupWriteRotatingFileHandler
    

    And then in LOGGING['handlers']['file'] you have

    'class': 'logging.handlers.GroupWriteRotatingFileHandler'
    

提交回复
热议问题