zoukankan      html  css  js  c++  java
  • log日志----logging模块

    # 配置log对象 稍微有点复杂 能做的事情相对多
    import logging
    logger = logging.getLogger()
    fh = logging.FileHandler('log.log',encoding='utf-8')
    sh = logging.StreamHandler()    # 创建一个屏幕控制对象
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s [line:%(lineno)d] : %(message)s')
    # 文件操作符 和 格式关联
    fh.setFormatter(formatter)
    sh.setFormatter(formatter2)
    # logger 对象 和 文件操作符 关联
    logger.addHandler(fh)
    logger.addHandler(sh)
    logging.debug('debug message')       # 低级别的 # 排错信息
    logging.info('info message')            # 正常信息
    logging.warning('警告错误')      # 警告信息
    logging.error('error message')          # 错误信息
    logging.critical('critical message') # 高级别的 # 严重错误信息

    优点:程序的充分解耦,让程序变得高可定制

    logging.basicConfig(level=logging.WARNING,
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S')
    try:
        int(input('num >>'))
    except ValueError:
        logging.error('输入的值不是一个数字')
    
    logging.debug('debug message')       # 低级别的 # 排错信息
    logging.info('info message')            # 正常信息
    logging.warning('warning message')      # 警告信息
    logging.error('error message')          # 错误信息
    logging.critical('critical message') # 高级别的 # 严重错误信息
    
    print('%(key)s'%{'key':'value'})
    print('%s'%('key','value'))

    # basicconfig 简单 能做的事情相对少
    # 中文的乱码问题
    # 不能同时往文件和屏幕上输出

    日志的作用:

    # logging
    # 我能够“一键”控制
    # 排错的时候需要打印很多细节来帮助我排错
    # 严重的错误记录下来
    # 有一些用户行为 有没有错都要记录下来
  • 相关阅读:
    0129 System类 Math类 Arrays类 大数据运算
    0127 基本类型包装类
    'telnet' 不是内部或外部命令,也不是可运行的程序 解决方案
    删除时报org.springframework.dao.DataIntegrityViolationException
    mapper自动识别驼峰配置 spring MVC
    spring Security如何debug源码
    公司tomcat项目启动
    java.util.ConcurrentModificationException: null 异常解决
    @Transactional 学习
    mangoDB初探
  • 原文地址:https://www.cnblogs.com/qinghuani/p/8342663.html
Copyright © 2011-2022 走看看