简体   繁体   English

Django / Python日志记录

[英]django/python logging

I have a pretty weird problem with my logging facility i use inside django/python. 我在django / python中使用的日志记录工具有一个很奇怪的问题。 The logging is not working anymore since i upgraded to django 1.3. 自从我升级到django 1.3后,日志记录不再起作用。 It seems to be related to the logging level and the 'debug=' setting in the settings.py file. 它似乎与日志记录级别和settings.py文件中的'debug ='设置有关。

1) When i log INFO messages and debug=False, the logging won't happen, my file doesn't get appended. 1)当我记录INFO消息并且debug = False时,将不会发生记录,我的文件也不会被追加。 2) When i log WARNING messages and debug=False, the logging works perfectly like i want it to, the file gets appended 3) When i log INFO messages and debug=True, the logging seems to work, the file get appended. 2)当我记录警告消息并且debug = False时,日志记录的工作方式与我想要的完全一样,文件将被追加3)当我记录信息消息并且debug = True时,日志记录似乎可以工作,文件将被追加。

How could i log INFO messages with debug=False? 我怎么用debug = False记录INFO消息? It worked before django 1.3... is there somewhere a mysterious setting which do the trick? 它在django 1.3之前就可以工作了……在某个神秘的地方有这个技巧吗? Underneath there is a sample code: 下面有一个示例代码:

views.py: views.py:

import logging

logging.basicConfig(level=logging.INFO,
                format='%(asctime)s %(levelname)s %(message)s',
                filename='/opt/general.log',
                filemode='a')


def create_log_file(filename, log_name, level=logging.INFO):
    handler = logging.handlers.TimedRotatingFileHandler(filename, 'midnight', 7, backupCount=10)
    handler.setLevel(level)
    formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s', '%a, %Y-%m-%d %H:%M:%S')
    handler.setFormatter(formatter)
    logging.getLogger(log_name).addHandler(handler)

create_log_file('/opt/test.log', 'testlog')

logger_test = logging.getLogger('testlog')

logger_test.info('testing the logging functionality')

With this code the logging does not work in Django 1.3 with debug set to False in the settings.py file. 使用此代码,在settings.py文件中将debug设置为False的情况下,日志记录在Django 1.3中不起作用。 When i should do like this: 当我应该这样做时:

logger_test.warning('testing the logging functionality')

This works perfectly when debug is set to False. 当debug设置为False时,这将完美工作。 The levels DEBUG and INFO aint logging but WARNING,ERROR and CRITICAL are doing their job... 级别DEBUG和INFO不会进行日志记录,但是WARNING,ERROR和CRITICAL正在发挥作用...

Does anyone have an idea? 有人有主意吗?

Since Django 1.3 contains its own logging configuration, you need to ensure that anything you're doing doesn't clash with it. 由于Django 1.3包含自己的日志记录配置,因此您需要确保所做的任何操作都不会与之冲突。 For example, if the root logger has handlers already configured by Django by the time your module first gets imported, your basicConfig() call won't have any effect. 例如,如果在首次导入模块时根记录器已经由Django配置了处理程序,则basicConfig()调用将无效。

What you're describing is the normal logging situation - WARNINGs and above get handled, while INFO and DEBUG are suppressed by default. 您所描述的是正常的日志记录情况-警告和以上内容得到处理,而INFO和DEBUG默认情况下被抑制。 It looks as if your basicConfig() is not having any effect; 看起来您的basicConfig()无效。 You should consider replacing your basicConfig() call with the appropriate logging configuration in settings.py , or at least investigate the root logging level and what handlers are attached to it, at the time of your basicConfig() call. 您应该考虑更换你的basicConfig()与相应的日志记录配置调用settings.py ,或者至少调查根本日志记录级别,哪些处理程序连接到它,在你的时间basicConfig()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM