zoukankan      html  css  js  c++  java
  • DJango错误日志生成

    DJango错误日志生成

    setting.py设置

    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.utils.log.RequireDebugTrue',
            },
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'filters': ['require_debug_true'],
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
            'file': {
                # 实际开发建议使用WARNING
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',
                # 日志位置,日志文件名,日志保存目录必须手动创建,注:这里的文件路径要注意BASE_DIR
                'filename': os.path.join(os.path.dirname(BASE_DIR), "logs/luffy.log"),
                # 日志文件的最大值,这里我们设置300M
                'maxBytes': 300 * 1024 * 1024,
                # 日志文件的数量,设置最大日志数量为10
                'backupCount': 10,
                # 日志格式:详细格式
                'formatter': 'verbose',
                # 编码
                'encoding': 'utf-8'
            },
        },
        # 日志对象
        'loggers': {
            'django': {
                'level': 'INFO',
                'handlers': ['console', 'file'],
                'propagate': True,  # 是否让日志信息继续冒泡给其他的日志处理系统
            },
        }
    }
    
    

    exception.py(抛错设置)

    # rest_framework.views 下的 exception_handler 处理了所有 drf可控范围内的异常
    from rest_framework.views import exception_handler as drf_exception_handler
    # drf的异常还是交给 drf_exception_handler,我们只需要处理 drf未处理的异常
    from rest_framework.response import Response
    from .response import APIResponse
    # 自定义异常句柄的原因:要通过 logging 记录异常日志
    from .logging import logger
    def exception_handler(exc, context):
        response = drf_exception_handler(exc, context)
    
        if response is None:
            # drf处理不了的异常
            error_info = '【%s】【%s】' % (context['view'], exc)
            logger.error(error_info)
            # return Response({
            #     'exception': '服务器异常',
            # }, status=500)
            return APIResponse(1, '服务器异常', status=500)
    
        response.exception = True
        return response
    
    

    logging.py

    import logging
    logger = logging.getLogger('django')
    
  • 相关阅读:
    shell进阶——expect免交互工具的使用
    SSH非交互式密码授权远程执行脚本---转载
    github新项目上传操作(下面的例子以centos7-redis-shell项目为实例)
    camstar跨平台开发
    C/C++语言中的int所能表示的最大值最小值
    剑指offer 15:二进制中1的个数
    剑指offer33:二叉搜索树的后序遍历序列
    剑指offer31:栈的压入弹出序列
    剑指offer:包含min函数的栈
    剑指offer50拓展:字符流中第一个不重复的字符
  • 原文地址:https://www.cnblogs.com/pythonywy/p/11569390.html
Copyright © 2011-2022 走看看