django中日志配置

# ======日志配置======
# NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL
LOGS_ROOT = os.path.join(BASE_DIR, 'logs')
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    # 日志格式
    'formatters': {
        'standard': {
            # 'format': '%(asctime)s [%(threadName)s:%(thread)s prototype] [%(name)s:%(lineno)s prototype] [%(levelname)s]- %(message)s'
            'format': '%(asctime)s [%(threadName)s:%(thread)s prototype] [%(name)s:%(lineno)s prototype] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'
        },
    },
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    # 用来定义具体处理日志的方式
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOGS_ROOT, 'all.log'),  # 日志输出文件
            'maxBytes': 1024 * 1024 * 5,  # 5 MB 文件大小
            'backupCount': 60,  # 备份份数
            'formatter': 'standard',  # 使用哪种日志格式
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # 输出到控制台
            'formatter': 'standard'
        },
        'request_handler': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',  # 输出到文件
            'filename': os.path.join(LOGS_ROOT, 'django_request.log'),
            'maxBytes': 1024 * 1024 * 5,  # 5 MB
            'backupCount': 60,
            'formatter': 'standard',
        },
        'exception_handler': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOGS_ROOT, 'exception.log'),
            'maxBytes': 1024 * 1024 * 5,  # 5 MB
            'backupCount': 60,
            'formatter': 'standard',
        },
    },
    # 配置用那种handlers来处理日志
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False
        },
        # 某个app专用
        'BangSo.app': {
            'handlers': ['default', 'console'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'INFO',
            'propagate': False
        },
        'exception': {
            'handlers': ['exception_handler'],
            'level': 'ERROR',
            'propagate': False
        },
    }
}

    解析:

        1.formatters:配置打印日志格式

        2.handlers  :用来定义具体处理日志的方式,可以定义多种,"default"就是默认方式,"console"就是打印到控制台方式。

        3.loggers     :用来配置用那种handlers来处理日志,比如你同时需要输出日志到文件、控制台。

log日志应用:

logger = logging.getLogger('BangSo.app') #刚才在setting.py中配置的logger
try:
    mysql= connectMysql('127.0.0.1', '3306', 'david')
except Exception,e:
            logger.default(e) #直接将错误写入到日志文件,default是“BangSo.app"logger中的handlers
       logger.console(e) #直接将错误输出到控制台上,console是“BangSo.app"logger中的handlers

猜你喜欢

转载自www.cnblogs.com/konglingxi/p/9438301.html