django日志的设置

关于django的日志设置详细可以看下官方文档:https://yiyibooks.cn/xx/Django_1.11.6/topics/logging.html

示例:

# 日志文件配置
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,  # 是否禁用已经存在的日志器
    'formatters': {                      # 日志信息显示的格式
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {      # django在debug模式下才输出日志
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {          # 日志处理方法
        'console': {      # 向终端中输出日志
            'level': 'INFO',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {          # 向文件中输出日志
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            # 日志文件的位置
            'filename': os.path.join(os.path.dirname(BASE_DIR), "logs/meiduo.log"),  
            'maxBytes': 300 * 1024 * 1024,   # 日志文件的最大容量
            'backupCount': 10,   # 300M * 10
            'formatter': 'verbose'
        },
    },
    'loggers': { 
        'django': {      # 定义了一个名为django的日志器
            'handlers': ['console', 'file'],      # 可以同时向终端与文件中输出日志
            'level': 'INFO',                      # 日志器接收的最低日志级别
        },
    }
}

如何记录日志:   

    import logging
    logger = logging.getLogger('django')
    logger.debug('xx')
    logger.info('xx')
    logger.warning('xx')
    logger.error('xx')
    logger.critical('xx')

日志级别:

    DEBUG:    用于调试目的的底层系统信息
    INFO:    普通的系统信息
    WARNING:表示出现一个较小的问题。
    ERROR:    表示出现一个较大的问题。
    CRITICAL:表示出现一个致命的问题。

原文地址:https://www.cnblogs.com/chichung/p/9925366.html