zoukankan      html  css  js  c++  java
  • python3 logging模块

    import logging
    
    
    """单一日志的简单配置,写入日志的编码目前默认是gbk"""
    logging.basicConfig(
        filename='xx.log',  # 日志文件
        format='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S',  # 日志时间格式
        level=30)  # 日志等级.30分以上的信息会记录到日志
    
    # 5个等级.
    logging.critical("严重信息")  # 50分
    logging.error("错误信息")  # 40分
    logging.warning("警告信息")  # 30分
    logging.info("普通信息")  # 20分
    logging.debug("详细信息")  # 10分
    
    logging.log(35, "自定义消息")  # 35分
    import logging
    
    
    """多个日志文件的配置"""
    file_handler = logging.FileHandler('user.log', 'a', encoding='utf-8')
    file_handler.setFormatter(logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s'))
    logger1 = logging.Logger('用户信息', level=logging.ERROR)
    logger1.addHandler(file_handler)  # 把logger和handler绑定在一起
    
    file_handler2 = logging.FileHandler('sys.log', 'a', encoding='utf-8')
    file_handler2.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s"))
    logger2 = logging.Logger('系统信息', level=logging.ERROR)
    logger2.addHandler(file_handler2)
    
    logger1.error('用户信息')
    logger2.error('系统信息')
    try:
        print(1/0)  # ZeroDivisionError: division by zero
        lst = [1, 2, 3]
        it = lst.__iter__()
        print(it.__next__())
        print(it.__next__())
        print(it.__next__())
        print(it.__next__())
    except StopIteration:
        print("迭代器没数据了")
    except ZeroDivisionError:
        print("除数不能为零")
    except Exception:
        print("程序出错了")
    finally:
        print("有没有错误都走这里")
    import logging
    import traceback
    
    
    """多个日志文件的配置"""
    file_handler = logging.FileHandler('user.log', 'a', encoding='utf-8')
    file_handler.setFormatter(logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s'))
    logger1 = logging.Logger('用户信息', level=logging.ERROR)
    logger1.addHandler(file_handler)  # 把logger和handler绑定在一起
    
    file_handler2 = logging.FileHandler('sys.log', 'a', encoding='utf-8')
    file_handler2.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s"))
    logger2 = logging.Logger('系统信息', level=logging.ERROR)
    logger2.addHandler(file_handler2)
    
    try:
        print(1/0)
    except Exception:
        logger1.error(traceback.format_exc())  # format_exc() 拿到错误位置
    finally:
        print("报不报错,都会走这里!")
  • 相关阅读:
    ionic 导航
    vscode多光标编辑(MAC)
    vscode保存文件时自动删除行尾空格
    ionic-native sqlite 插件5.x版的在ionic3.x上报错 cannot read property 'split' of undefined
    MAC OSX 自带Apache 配置及使用
    ionic中ion-item下的div,span,p不渲染,应该给这些元素加上item-content属性
    开发ionic + cordova应用时遇到的坑,resources/splash.png do not meet minimum size requirements: 2732x2732
    ionic 创建指令的命名规则
    Soldier and Badges (set的检索简单运用)
    打败大魔王之最小排列数问题(全排列)
  • 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11954226.html
Copyright © 2011-2022 走看看