Python登录Django [英] Python logging in Django

查看:112
本文介绍了Python登录Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Django应用程序,我正在尝试使用Python的日志记录模块进行错误/跟踪记录。理想情况下,我想为网站的不同区域配置不同的记录器。到目前为止,我已经有了所有这些工作,但有一件事让我刮目相看。

I'm developing a Django app, and I'm trying to use Python's logging module for error/trace logging. Ideally I'd like to have different loggers configured for different areas of the site. So far I've got all of this working, but one thing has me scratching my head.

我有根记录器进入sys.stderr,我已经配置另一个记录器写入一个文件。这是在我的settings.py文件中:

I have the root logger going to sys.stderr, and I have configured another logger to write to a file. This is in my settings.py file:

sviewlog = logging.getLogger('MyApp.views.scans')
view_log_handler = logging.FileHandler('C:\\MyApp\\logs\\scan_log.log')
view_log_handler.setLevel(logging.INFO)
view_log_handler.setFormatter(logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s'))
sviewlog.addHandler(view_log_handler)

看起来很简单。这是问题,但是,无论我写入sviewlog是否写入日志文件两次。根记录器只打印一次。就像addHandler()被调用两次。当我把代码放在一个调试器中时,这正是我看到的。 settings.py中的代码正在执行两次,因此将创建两个FileHandler并将其添加到同一个记录器实例中。但为什么?我该如何解决?

Seems pretty simple. Here's the problem, though: whatever I write to the sviewlog gets written to the log file twice. The root logger only prints it once. It's like addHandler() is being called twice. And when I put my code through a debugger, this is exactly what I see. The code in settings.py is getting executed twice, so two FileHandlers are created and added to the same logger instance. But why? And how do I get around this?

任何人都可以告诉我这里发生了什么?我已经尝试将sviewlog记录器/处理程序实例化代码移动到使用它的文件(因为这实际上似乎是我适当的地方),但是我也有同样的问题。我在网上看到的大多数例子只使用根记录器,而我更喜欢有多个记录器。

Can anyone tell me what's going on here? I've tried moving the sviewlog logger/handler instantiation code to the file where it's used (since that actually seems like the appropriate place to me), but I have the same problem there. Most of the examples I've seen online use only the root logger, and I'd prefer to have multiple loggers.

推荐答案

请允许我回答我自己的问题。这里的根本问题是settings.py被导入两次,甚至更多(参见这里)。 (我还是不明白为什么会这样,也许一些Django专家可以向我解释)。这似乎也是一些其他的模块。在这一点上,我认为对于将要导入settings.py的次数进行假设是不明智的。对于这一点,这种假设一般不安全。我已经在除了settings.py之外的地方使用了这个代码,结果是相似的。

Allow me to answer my own question. The underlying problem here is that settings.py gets imported twice, or maybe even more (See here). (I still don't understand why this is. Maybe some Django expert could explain that to me.) This seems to be true of some other modules as well. At this point I don't think it's wise to make assumptions about how many times settings.py will be imported. For that matter, such assumptions aren't safe in general. I've had this code in places other than settings.py, and the results are similar.

你必须围绕这个代码进行编码。也就是说,在添加其他处理程序之前,您必须检查现有处理程序的记录器。这是一个有点丑陋,因为将多个处理程序(即使是同一类型)附加到一个记录器是完全合理的。有几个解决方案来处理这个问题。一个是检查你的记录器对象的处理程序属性。如果你只需要一个处理程序,并且你的长度> 0,那么不要添加它。个人我不喜欢这个解决方案,因为它会让更多的处理程序变得混乱。

You have to code around this. That is, you have to check your logger for existing handlers before adding additional handlers to it. This is a bit ugly because it's perfectly reasonable to have multiple handlers -- even of the same type -- attached to one logger. There are a few solutions to dealing with this. One is check the handlers property of your logger object. If you only want one handler and your length > 0, then don't add it. Personally I don't love this solution, because it gets messy with more handlers.

我喜欢这样的事情(感谢Thomas Guettler):

I prefer something like this (thanks to Thomas Guettler):

# file logconfig.py
if not hasattr(logging, "set_up_done"):
    logging.set_up_done=False

def set_up(myhome):
    if logging.set_up_done:
        return
    # set up your logging here
    # ...
    logging.set_up_done=True

我必须说,我希望Django多次导入settings.py的事实更好地记录在案。我可以想像,我的配置是以某种方式导致这种多重导入的,但我无法找出导致问题的原因和原因。也许我在他们的文件中找不到,但我认为这是您需要向用户发出警告的那种事情。

I must say, I wish the fact that Django imports settings.py multiple times were better documented. And I would imagine that my configuration is somehow cause this multiple import, but I'm having trouble finding out what is causing the problem and why. Maybe I just couldn't find that in their documents, but I would think that's the sort of thing you need to warn your users about.

这篇关于Python登录Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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