在 Django 中优雅地设置 Python 日志记录 [英] Elegant setup of Python logging in Django

查看:31
本文介绍了在 Django 中优雅地设置 Python 日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到一种让我满意的使用 Django 设置 Python 日志记录的方法.我的要求相当简单:

I have yet to find a way of setting up Python logging with Django that I'm happy with. My requirements are fairly simple:

  • 针对不同事件的不同日志处理程序 - 也就是说,我希望能够记录到不同的文件
  • 在我的模块中轻松访问记录器.该模块应该能够轻松找到它的记录器.
  • 应该很容易适用于命令行模块.系统的一部分是独立的命令行或守护进程.使用这些模块应该可以轻松使用日志记录.

我当前的设置是使用 logging.conf 文件,并在我登录的每个模块中设置日志记录.感觉不太对.

My current setup is to use a logging.conf file and setup logging in each module I log from. It doesn't feel right.

您有喜欢的日志记录设置吗?请详细说明:您如何设置配置(您是使用 logging.conf 还是在代码中设置),您在何处/何时启动记录器,以及您如何在你的模块等

Do you have a logging setup that you like? Please detail it: how do you setup the configuration (do you use logging.conf or set it up in code), where/when do you initiate the loggers, and how do you get access to them in your modules, etc.

推荐答案

目前我发现的最好方法是在 settings.py 中初始化日志设置——别无他法.您可以使用配置文件或以编程方式逐步进行 - 这仅取决于您的要求.关键是我通常将我想要的处理程序添加到根记录器,使用级别,有时使用 logging.Filters 来获取我想要的事件到适当的文件、控制台、系统日志等.当然,您可以将处理程序添加到任何其他记录器也是,但根据我的经验,通常不需要这样做.

The best way I've found so far is to initialize logging setup in settings.py - nowhere else. You can either use a configuration file or do it programmatically step-by-step - it just depends on your requirements. The key thing is that I usually add the handlers I want to the root logger, using levels and sometimes logging.Filters to get the events I want to the appropriate files, console, syslogs etc. You can of course add handlers to any other loggers too, but there isn't commonly a need for this in my experience.

在每个模块中,我使用

logger = logging.getLogger(__name__)

并使用它来记录模块中的事件(并且,如果我想进一步区分)使用一个记录器,它是上面创建的记录器的子代.

and use that for logging events in the module (and, if I want to differentiate further) use a logger which is a child of the logger created above.

如果我的应用程序可能会在没有在 settings.py 中配置日志记录的站点中使用,我会在某处定义一个 NullHandler,如下所示:

If my app is going to be potentially used in a site which doesn't configure logging in settings.py, I define a NullHandler somewhere as follows:

#someutils.py

class NullHandler(logging.Handler):
    def emit(self, record):
        pass

null_handler = NullHandler()

并确保将它的一个实例添加到我的应用程序中使用日志记录的模块中创建的所有记录器中.(注意:NullHandler 已经在 Python 3.1 的日志包中,并将在 Python 2.7 中.)所以:

and ensure that an instance of it is added to all loggers created in the modules in my apps which use logging. (Note: NullHandler is already in the logging package for Python 3.1, and will be in Python 2.7.) So:

logger = logging.getLogger(__name__)
logger.addHandler(someutils.null_handler)

这样做是为了确保您的模块在未在 settings.py 中配置日志记录的站点中正常运行,并且您不会收到任何烦人的找不到记录器 XYZ 的处理程序"消息(这些消息是有关可能错误配置的日志记录的警告).

This is done to ensure that your modules play nicely in a site which doesn't configure logging in settings.py, and that you don't get any annoying "No handlers could be found for logger X.Y.Z" messages (which are warnings about potentially misconfigured logging).

这样做符合您规定的要求:

Doing it this way meets your stated requirements:

  • 您可以像目前一样为不同的事件设置不同的日志处理程序.
  • 轻松访问模块中的记录器 - 使用 getLogger(__name__).
  • 易于应用于命令行模块 - 它们还导入 settings.py.

更新:请注意,从 1.3 版开始,Django 现在合并了 支持日志记录.

Update: Note that as of version 1.3, Django now incorporates support for logging.

这篇关于在 Django 中优雅地设置 Python 日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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