为什么Django日志记录会跳过日志条目? [英] Why Django logging skips log entries?

查看:102
本文介绍了为什么Django日志记录会跳过日志条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的设置模块:

LOGGING = {
  'version': 1,
  'disable_existing_loggers': False,
  'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': '/django-python/django/testapp/testapp.log',
    },
  },
  'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
  },
}

这是我在视图文件中的代码:

and this is my code in a view file:

import logging
logger = logging.getLogger(__name__)
logger.info("this is an error message!!")

我从各个模块获取以前的日志,但没有上面的日志条目这是一条错误消息".

I am getting the previous logs from various modules but not the above log entry "this is an error message".

推荐答案

您的日志记录配置仅捕获django名称空间内的日志.

Your logging configuration only captures logs within the django namespace.

此行:

logger = logging.getLogger(__name__)

...告诉记录器将您模块的名称用作这些日志的名称空间(文档).如果您的模块名为mymodule,则可以通过在日志记录配置中添加类似的内容来捕获这些日志:

... tells the logger to use your module's name as the namespace for these logs (docs). If your module is called mymodule, then you can catch these logs by adding something like this to your logging configuration:

'loggers': {
   'django' : {...},
   'mymodule': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},

这篇关于为什么Django日志记录会跳过日志条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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