Django Log使用

2018-03-29  本文已影响0人  HeJD

settings设置

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
     'formatters': {
        'standard': {
            'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(levelname)s]- %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            //需要先创建*.log文件,并设置相应权限
            'filename': PROJECT_ROOT+'/log/dnafw.log',
            //*.log文件的最大值,避免后期由于文件过大而无法查看
            'maxBytes': 1024*1024*2, # 5 MB
            'backupCount': 5,
            //与上面的formatter设置对应
            'formatter':'standard',
        },
    },
    'loggers': {
        'my_debug':{
        //与上面的handlers对应
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True        
        },
    }
}

views中使用

import loggin

log = logging.getLogger("my_debug")
log.debug("***")
上一篇下一篇

猜你喜欢

热点阅读