• 28-2 logging 日志模块


    功能
    1. 日志格式的规范
    2. 操作的简化
    3. 日志的分级管理


    logging不能帮你做的事情
    自动生成你要打印的内容
    需要程序员自己在开发的时候定义好 :
    在哪些地方需要打印,要打印的内容是什么,内容的级别

    logging模块的使用 :
    普通配置型 简单的 可定制化差
    对象配置型 复杂的 可定制化强


    一、认识日志分级

    import logging
    logging.debug('debug message')      # 调试模式  一般不显示  10
    logging.info('info message')        # 基础信息    一般不显示 20
    logging.warning('warning message')  # 警告    30
    logging.error('error message')      # 错误  40
    logging.critical('critical message')# 严重错误  50
    
    +++++++++++++++++++++++
    WARNING:root:warning message
    ERROR:root:error message
    CRITICAL:root:critical message

    上面debuginfo默认不显示,但是实在要显示咋办?

    配置日志级别
    import logging
    
    logging.basicConfig(level=logging.DEBUG)   # 10 配置日志级别
    logging.debug('debug message')      # 调试模式
    logging.info('info message')        # 基础信息
    logging.warning('warning message')  # 警告
    logging.error('error message')      # 错误
    logging.critical('critical message')# 严重错误

    设置格式,输出到当前屏幕上

    import logging
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',
                        )
    logging.debug('debug message')      # 调试模式
    logging.info('info message')        # 基础信息
    logging.warning('warning message')  # 警告
    logging.error('error message')      # 错误
    logging.critical('critical message')# 严重错误
    
    =====================
    Wed, 08 Apr 2020 22:18:40 8.logging模块.py[line:39] DEBUG debug message
    Wed, 08 Apr 2020 22:18:40 8.logging模块.py[line:40] INFO info message
    Wed, 08 Apr 2020 22:18:40 8.logging模块.py[line:41] WARNING warning message
    Wed, 08 Apr 2020 22:18:40 8.logging模块.py[line:42] ERROR error message
    Wed, 08 Apr 2020 22:18:40 8.logging模块.py[line:43] CRITICAL critical message

    配置输入到文件内    添加 filename='test.log'

    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',
                        filename='test.log')

    核心矛盾: 只能输出屏幕和文件二选一  这点无法避免

    所以用下面:

      二、basicConfig

    logger对象的形式来操作日志文件

    创建一个logger对象  
    logger = logging.getLogger()
      
    创建一个文件管理操作符
    fh = logging.FileHandler('logger.log',encoding='utf-8')   # 输出中文

    创建一个屏幕管理操作符
    sh = logging.StreamHandler()

    创建一个日志输出的格式
    format1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
     实例化的过程。这四个之间没有任何关系!



    所以我们:
      
    文件管理操作符 绑定一个 格式
    fh.setFormatter(format1)

      屏幕管理操作符 绑定一个 格式
    sh.setFormatter(format1)
    logger.setLevel(logging.DEBUG)    # 配置日志级别
    
    
      logger对象 绑定 文件管理操作符
    logger.addHandler(fh)
      logger对象 绑定 屏幕管理操作符
    logger.addHandler(sh)
    logger.debug('debug message')      # 调试模式
    logger.info('我的信息')        # 基础信息
    logger.warning('warning message')  # 警告
    logger.error('error message')      # 错误
    logger.critical('critical message')# 严重错误
    View Code
  • 相关阅读:
    利用进制转换压缩数字
    一个不错的移动政务网站
    系统缓存全解析(转摘)
    GeoDatabase元数据不能编辑的问题“The application is not licensed to perform this operation ”
    RSA算法的分析与实现(转载)
    云计算经济学
    Memcached在Windows操作系统下多实例并存(转载)
    AGS FlexView 与 ArcGIS Server的FeatureLayer交互的过程
    转载>ArcEngine中版本的使用
    基于ArcEngine的地图四着色算法的实现(转载)
  • 原文地址:https://www.cnblogs.com/zhuangdd/p/12663624.html
Copyright © 2020-2023  润新知