Django 中的 日志处理

日志处理:

  • 上线后必须使用 便于以后的 维护 管理 根据日志 处理 BUG

在 项目中 定义一个 存放日志的 文件夹 log 存放所有 等级 的 日志

配置: 将下面的日志的 配置 写入 django 的 settings 中

# 日志配置
# 日志存放路径
BASE_LOG_DIR = os.path.join(BASE_DIR, "log")

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应用如下配置
            'handlers': ['SF', 'console', 'error'],  # 上线之后可以把'console'移除
            'level': 'DEBUG',
            'propagate': True,  # 是否向上一级logger实例传递日志信息
        },
        'collect': {  # 名为 'collect' 的logger还单独处理
            'handlers': ['console', 'collect'],
            'level': 'INFO',
        }
    },
}

日志流程图:

日志使用: 使用日志 打印 信息 替代 print() 函数

  • 在需要打印日志的地方使用

    • 注意 日志只能 处理单条内容 logger.debug(a, b) 会报错
    # 导入 日志模块
    import logging
    # 实例化log对象 --> logger
    # 以当前文件的名字作为logger实例的名字
    logger = logging.getLogger(__name__)
    # 或者  生成一个 名字叫 collect 的日志实例  这个名字随便指定   单需要在 日志的 配置中 设置配置 
    logger_c = logging.getLogger('collect')
    
    打印 代替 print() 
    logger.debug(request.data or error)  # 伪代码
    
    记录日志 在需要记录的  地方 
    logger.error(str(e))
     
    
    根据要记录的  日志等级 区分保存的 位置
    

类补充:

__slots__ = ('code', 'data', 'error') # 限制 类中 只有这三个属性

  • 设置之后 类中就没有 __dict__ 方法了 只限制本类 对子类没有限制

**数据库补充 : **

数据库设计三大范式:

  1. 原子性:每一列的数据不能重复
  2. 表中每一个字段都要和主键有关联
  3. 表中的字段要和主键是直接关系不能间接关系
posted @ 2019-02-27 21:47  拐弯  阅读(1190)  评论(0编辑  收藏  举报