zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    2013.4.15 Particle Swarm Optimization with Skyline Operator for Fast Cloudbased Web Service Composition
    Adaptive service composition in flexible processes
    2013.4.13 DomainSpecific Service Selection for Composite Services
    2013.4.14 Modeling and Algorithms for QoSAware Service Composition in VirtualizationBased Cloud Computing
    2013.5.29 Towards Networkaware Service Composition in the Cloud
    Efficient algorithms for Web services selection with endtoend QoS constraints
    SQL Server中常用的SQL语句
    接口限流自定义注解
    linux服务器生产环境搭建
    MVEL自定义函数重复掉用报错:duplicate function
  • 原文地址:https://www.cnblogs.com/zhuangdd/p/12663624.html
Copyright © 2011-2022 走看看