zoukankan      html  css  js  c++  java
  • 0009 Django工程配置(06 Django logging日志配置)

      Django自带日志处理。日志不但方便调试,而且方便在项目上线后,查看报错信息。

      日志器的规划非常重要,一般来说,项目中每个APP都应该对应一个日志器,而一个日志器对应两个日志文件:

      INFO日志文件:用于程序员输出信息

      ERROR日志文件:用于输出系统报错信息

    1 日志配置

      在settings.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在debug模式下才输出日志
                '()': 'django.utils.log.RequireDebugTrue',
            },
        },
        'handlers': {  # 日志处理方法
            'console': {  # 向终端中输出日志
                'level': 'INFO',
                'filters': ['require_debug_true'],
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
            'org_info': {  # Organizations APP日志文件配置
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/org_info.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
            'org_error': {  # Organizations APP日志文件配置
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/org_error.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
            'exp_info': {  # Examples APP日志文件配置
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/exp_info.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
            'exp_error': {  # Examples APP日志文件配置
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/exp_error.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
            'gen_info': {  # GeneralTools APP日志文件配置
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/gen_info.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
            'gen_error': {  # GeneralTools APP日志文件配置
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/gen_error.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
            'wec_info': {  # GeneralTools APP日志文件配置
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/wec_info.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
            'wec_error': {  # GeneralTools APP日志文件配置
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(BASE_DIR, 'Configurations/log/wec_error.log'),  # 日志文件的位置
                'maxBytes': 300 * 1024 * 1024,
                'backupCount': 10,
                'formatter': 'verbose'
            },
        },
        'loggers': {  # APP日志器
            'Organizations': {  # Organizations APP 日志器
                'handlers': ['console', 'org_info', 'org_error'],  # 可以同时向终端与文件中输出日志
                'propagate': True,  # 是否继续传递日志信息
                'level': 'INFO',  # 日志器接收的最低日志级别
            },
            'Examples': {  # Examples APP 日志器
                'handlers': ['console', 'exp_info', 'exp_error'],  # 可以同时向终端与文件中输出日志
                'propagate': True,  # 是否继续传递日志信息
                'level': 'INFO',  # 日志器接收的最低日志级别
            },
            'GeneralTools': {  # GeneralTools APP 日志器
                'handlers': ['console', 'gen_info', 'gen_error'],  # 可以同时向终端与文件中输出日志
                'propagate': True,  # 是否继续传递日志信息
                'level': 'INFO',  # 日志器接收的最低日志级别
            },
            'Wechat': {  # GeneralTools APP 日志器
                'handlers': ['console', 'wec_info', 'wec_error'],  # 可以同时向终端与文件中输出日志
                'propagate': True,  # 是否继续传递日志信息
                'level': 'INFO',  # 日志器接收的最低日志级别
            },
    
        }
    } 

    2 日志器的调用

    import logging
    
    logger = logging.getLogger('GeneralTools') # 调用日志器
    

    3 日志器的使用

      日志器使用有两种情况:

    3.1 输出编程信息

      在程序编制过程中,需要输出一些信息时,可把控制台信息输出到日志。每次运行,控制台信息会被清空,而日志不会清空,调试信息显示在日志里,更方便查阅历史信息。

    logger.info('===========================>')
    

    3.2 输出异常信息

      异常信息一般是在try...except中输出。也可以在raise之前输出。

    try:
            #...代码
    except Exception as e:
            # 输出到日志
            logger.error(str(e))
    
    # 或在raise前输出
    logger.error('失败')
    raise Exception('...')
    

    4 显示效果

      

  • 相关阅读:
    pyinstaller 打包后无法运行
    Android Uiautomator2 gradlew 坑
    JNDI 在 J2EE 中的角色
    23种设计模式
    Struts2工作原理
    SpringMVC工作原理
    堆内存设置
    安装和使用 memcached
    SQL面试题及答案
    30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
  • 原文地址:https://www.cnblogs.com/dorian/p/12407882.html
Copyright © 2011-2022 走看看