• 日志应用


    做开发离不开日志,以下是我在工作中写Django项目常用的logging配置。

    # 日志配置
    BASE_LOG_DIR = os.path.join(BASE_DIR, "log")  # 根目录,在项目里面创建一个目录放日志
    # django提供的一个配置项
    LOGGING = {
        'version': 1,  # 保留字
        'disable_existing_loggers': False,  # 是否禁用已经存在的日志实例
        'formatters': {  # 定义日志的格式
            'standard': {
                'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                          '[%(levelname)s][%(message)s]'
            },
            'simple': {
                'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
            },
            'collect': {
                'format': '%(message)s'
            }
        },
        'filters': {  # 定义日志的过滤器
            'require_debug_true': {
                '()': 'django.utils.log.RequireDebugTrue',
            },
        },
        'handlers': {  # 日志处理程序
            'console': {
                'level': 'DEBUG',
                'filters': ['require_debug_true'],  # 只有在Django debug为True时才在屏幕打印日志
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
            'SF': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,根据文件大小自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 500,  # 日志大小 50M(最好不要超过1G)
                'backupCount': 3,  # 备份数为3  xx.log --> xx.log.1 --> xx.log.2 --> xx.log.3
                'formatter': 'standard',
                'encoding': 'utf-8',  # 文件记录的编码格式
            },
            'TF': {
                'level': 'INFO',
                'class': 'logging.handlers.TimedRotatingFileHandler',  # 保存到文件,根据时间自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"),  # 日志文件
                'backupCount': 3,  # 备份数为3  xx.log --> xx.log.2018-08-23_00-00-00 --> xx.log.2018-08-24_00-00-00 --> ...
                'when': 'D',  # 每天一切, 可选值有S/秒 M/分 H/小时 D/天 W0-W6/周(0=周一) midnight/如果没指定时间就默认在午夜
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            'error': {
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_err.log"),  # 日志文件
                'maxBytes': 1024 * 1024 * 5,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'standard',
                'encoding': 'utf-8',
            },
            'collect': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,自动切
                'filename': os.path.join(BASE_LOG_DIR, "xxx_collect.log"),
                'maxBytes': 1024 * 1024 * 50,  # 日志大小 50M
                'backupCount': 5,
                'formatter': 'collect',
                'encoding': "utf-8"
            }
        },
        'loggers': {  # 日志实例
            '': {  # 默认的logger应用如下配置
                '''
                SF 文件   console终端   错误error
                '''
                'handlers': ['SF', 'console', 'error'],  # 上线之后可以把'console'移除
                'level': 'DEBUG',
                'propagate': True,  # 是否向上一级logger实例传递日志信息
            },
            'collect': {  # 名为 'collect' 的logger还单独处理
                'handlers': ['console', 'collect'],
                'level': 'INFO',
            }
        },
    }
    

      

    应用

    # 导入
    import logging
    # 实例化log对象 -->logger
    # llogger = logging.getLogger('这个名字要和配置的名字匹配,在loggers里可以指定用什么处理')
    logger = logging.getLogger('__name__')#以当前文件的名字创建实例
    
    # 使用
    logger.error(str('返回什么错误'))
    # logging.debug() == print()看错

      

    附:Python logger流示图

  • 相关阅读:
    USDT与omniCore钱包
    C# 事件(第四章)
    委托进阶(第三章)
    委托入门(第二章)
    委托入门(第一章)
    asp.net页面生命周期
    在WEB程序中小心使用"ThreadStatic"
    如何在一个请求中共享数据或对象实例
    .net垃圾回收机制原理
    MVC模式简介
  • 原文地址:https://www.cnblogs.com/xihuanniya/p/10295991.html
Copyright © 2020-2023  润新知