• django 完整日志配置


    django中的log需要在settings.py中配置

    import time
    
    cur_path = os.path.dirname(os.path.realpath(__file__))  # log_path是存放日志的路径
    log_path = os.path.join(os.path.dirname(cur_path), 'logs')
    if not os.path.exists(log_path): os.mkdir(log_path)  # 如果不存在这个logs文件夹,就自动创建一个
    
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            # 日志格式
            'standard': {
                'format': '[%(asctime)s] [%(filename)s:%(lineno)d] [%(module)s:%(funcName)s] '
                          '[%(levelname)s]- %(message)s'},
            'simple': {  # 简单格式
                'format': '%(levelname)s %(message)s'
            },
        },
        # 过滤
        'filters': {
        },
        # 定义具体处理日志的方式
        'handlers': {
            # 默认记录所有日志
            'default': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(log_path, 'all-{}.log'.format(time.strftime('%Y-%m-%d'))),
                'maxBytes': 1024 * 1024 * 5,  # 文件大小
                'backupCount': 5,  # 备份数
                'formatter': 'standard',  # 输出格式
                'encoding': 'utf-8',  # 设置默认编码,否则打印出来汉字乱码
            },
            # 输出错误日志
            'error': {
                'level': 'ERROR',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(log_path, 'error-{}.log'.format(time.strftime('%Y-%m-%d'))),
                'maxBytes': 1024 * 1024 * 5,  # 文件大小
                'backupCount': 5,  # 备份数
                'formatter': 'standard',  # 输出格式
                'encoding': 'utf-8',  # 设置默认编码
            },
            # 控制台输出
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'standard'
            },
            # 输出info日志
            'info': {
                'level': 'INFO',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': os.path.join(log_path, 'info-{}.log'.format(time.strftime('%Y-%m-%d'))),
                'maxBytes': 1024 * 1024 * 5,
                'backupCount': 5,
                'formatter': 'standard',
                'encoding': 'utf-8',  # 设置默认编码
            },
        },
        # 配置用哪几种 handlers 来处理日志
        'loggers': {
            # 类型 为 django 处理所有类型的日志, 默认调用
            'django': {
                'handlers': ['default', 'console'],
                'level': 'INFO',
                'propagate': False
            },
            # log 调用时需要当作参数传入
            'log': {
                'handlers': ['error', 'info', 'console', 'default'],
                'level': 'INFO',
                'propagate': True
            },
        }
    }

    注释很详细,就不多做介绍了。

    脚本中调用:

    import logging
    
    logger = logging.getLogger('log')
    
    --skip-- logger.info(
    '请求成功! response_code:{};response_headers:{};response_body:{}'.format(response_code, response_headers, response_body[:251])) logger.error('请求出错:{}'.format(error))

    生成日志:

    all.log

     

    info.log

    error.log

  • 相关阅读:
    小、快、简、易、强的“银弹”— fastm
    使用Creative suite 3和Flex Builder3实现Flex 3的换肤
    Apache HTTP Server 与 Tomcat 的三种连接方式介绍
    iframe自动适应付窗口的大小变换
    Flash网络游戏开发入门经验共享
    比较详细的 Linux Top 命令解析
    HttpContext是干什么的
    asp.net,cookie,写cookie,取cookie 的方法
    为什么我们不要 .NET 程序员
    在Ubuntu上安装使用深度影音&深度音乐(推荐)
  • 原文地址:https://www.cnblogs.com/changqing8023/p/9639769.html
Copyright © 2020-2023  润新知