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

前端 未结 7 568
太阳男子
太阳男子 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:11

    It looks that def _open(self): worked with umask(0o000) to get all the permissions of -rw-rw-rw-.

    os.chmod(self.baseFilename, 0o0777) failed with ValueError: Unable to configure handler 'some_handler': [Errno 1] Operation not permitted: if the log file has ownership of root:root that's different from running process's, such as testuser.

    from logging import handlers
    
    import logging
    import os
    
    class GroupWriteRotatingFileHandler(handlers.RotatingFileHandler):
        def _open(self):
            prevumask = os.umask(0o000)  # -rw-rw-rw-
            rtv = logging.handlers.RotatingFileHandler._open(self)
            os.umask(prevumask)
            return rtv
    
    LOGGING = {
        'handlers': {
            'db_handler': {
                    'level': 'DEBUG',
                    'class': 'log.GroupWriteRotatingFileHandler',
                    'filename': PATH_TO_LOGS + '/db.log',
                    'maxBytes': maxBytes,
                    'backupCount': backupCount,
                    'formatter': 'standard',
            },
    

    Log files:

    logs]# ls -lrt
    -rw-rw-rw- 1 root root 71 Apr  1 16:02 db.log
    
    logs]# ls -lrt
    total 0
    -rw-rw-rw- 1 testuser testuser 0 Apr  1 16:20 db.log
    
    

提交回复
热议问题