Django 设置默认日志记录 [英] Django Setup Default Logging

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

问题描述

我似乎不知道如何为我的 Django 安装设置默认"记录器.我想在 settings.py 中使用 Django 1.3 的新 LOGGING 设置.

I can't seem to figure out how to setup a "default" logger for my Django installation. I would like to use Django 1.3's new LOGGING setting in settings.py.

我查看了 Django Logging Doc 的示例,但在我看来,他们只设置处理程序,这些处理程序将为特定记录器进行日志记录.在他们的示例中,他们为名为django"、django.request"和myproject.custom"的记录器设置了处理程序.

I've looked at the Django Logging Doc's example, but it looks to me like they only setup handlers which will do logging for particular loggers. In the case of their example they setup handler for the loggers named 'django','django.request', and 'myproject.custom'.

我想要做的就是设置一个默认的 logging.handlers.RotatingFileHandler,它将默认处理所有记录器.即,如果我在我的项目中的某处创建了一个新模块,并且它由类似的内容表示:my_app_name.my_new_module,我应该能够做到这一点,并将所有日志记录转到旋转文件日志.>

All I want to do is setup a default logging.handlers.RotatingFileHandler which will handle all loggers by default. i.e., if I make a new module somewhere in my project and it is denoted by something like: my_app_name.my_new_module, I should be able to do this and have all logging goto the rotating file logs.

# In file './my_app_name/my_new_module.py'
import logging
logger = logging.getLogger('my_app_name.my_new_module')
logger.debug('Hello logs!') # <-- This should get logged to my RotatingFileHandler that I setup in `settings.py`!

推荐答案

想通了...

您通过使用空字符串引用它来设置捕获所有"记录器:''.

You set the 'catch all' logger by referencing it with the empty string: ''.

例如,在以下设置中,我将所有日志事件保存到 logs/mylog.log,除了 django.request 日志事件将保存到 logs/django_request.log.因为对于我的 django.request 记录器,'propagate' 设置为 False,日志事件将永远不会到达 'catch all' 记录器.

As an example, in the following setup I have the all log events getting saved to logs/mylog.log, with the exception of django.request log events which will be saved to logs/django_request.log. Because 'propagate' is set to False for my django.request logger, the log event will never reach the the 'catch all' logger.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': 'logs/mylog.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },  
        'request_handler': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': 'logs/django_request.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
    }
}

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

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