python logging.handlers.RotatingFileHandler是否允许创建组可写日志文件? [英] Does python logging.handlers.RotatingFileHandler allow creation of a group writable log file?

查看:560
本文介绍了python logging.handlers.RotatingFileHandler是否允许创建组可写日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux系统上使用标准的python(2.5.2)日志记录模块,特别是RotatingFileHandler.我的应用程序同时支持命令行界面和Web服务界面.我想将两者都写入同一个日志文件.但是,当日志文件轮换时,新文件具有644权限,并由Web服务器用户拥有,这会阻止命令行用户对其进行写入.我可以指定新的日志文件在日志记录配置中还是在日志记录初始化期间应该是可组写的?

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?

我已经研究了模式"设置(r/w/a),但是它似乎不支持任何文件权限.

I have looked into the 'mode' setting (r/w/a), but it doesn't seem to support any file permissions.

推荐答案

我诉诸于logging.handlers模块的扫描,无法看到指定其他文件权限模式的任何方法.因此,我现在有一个基于扩展RotatingFileHandler作为自定义处理程序的解决方案.一旦我找到了一些有关创建它的不错的参考,那是相当轻松的.自定义处理程序的代码如下.

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.

class GroupWriteRotatingFileHandler(handlers.RotatingFileHandler):

    def doRollover(self):
        """
        Override base class method to make the new log file group writable.
        """
        # Rotate the file first.
        handlers.RotatingFileHandler.doRollover(self)

        # Add group write to the current permissions.
        currMode = os.stat(self.baseFilename).st_mode
        os.chmod(self.baseFilename, currMode | stat.S_IWGRP)

我还发现,要从日志记录配置文件中引用自定义处理程序,我必须将模块绑定到日志记录名称空间.操作简单,但很烦人.

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.

from mynamespace.logging import custom_handlers
logging.custom_handlers = custom_handlers

我发现参考文献很有用: 绑定自定义处理程序创建自定义处理程序

References I found useful: binding custom handlers and creating custom handlers

这篇关于python logging.handlers.RotatingFileHandler是否允许创建组可写日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆