zoukankan      html  css  js  c++  java
  • 日志相关

    import logging,time

    # 1.使用logging提供的模块级别的函数
    format="%(asctime)s %(name)s 文件:%(filename)s 函数:%(funcName)s 行号:%(lineno)d %(levelname)s %(message)s"
    logging.basicConfig(format=format,
    datefmt="%Y/%m/%d %H:%M:%S",
    level=logging.DEBUG,
    filename='{}_log.txt'.format(time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime()).encode('gbk')))
    logging.debug("debug级别的错误信息")

    def sum(a,b):
    try:
    sum=a+b
    except Exception as error:
    logging.debug(error)

    sum('b',1)

    # 2.第二种方式是使用logging日志系统的四大组件
    #日志器logger、处理器handler、过滤器filter、格式器formattor

    # 关系:日志器是入口,一个日志器可以多个处理器,每个处理器都有对应的过滤器和格式器

    logger = logging.getLogger("logger")
    #日志输出的最低级别(忽略当前最低级别以下级别的日志信息)
    logger.setLevel(logging.ERROR)
    # 创建控制台处理
    sh=logging.StreamHandler()
    # 创建文件处理器
    fh=logging.FileHandler(filename="{}_log2.txt".format(time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime())),encoding="utf-8")
    # 把控制台处理器添加到日志器中
    logger.addFilter(sh)
    # 指定格式器显示的格式
    formatter = logging.Formatter(
    fmt="%(asctime)s %(name)s ,文件: %(filename)s 函数: %(funcName)s ,行号:%(lineno)d , %(levelname)s %(message)s",
    datefmt="%Y/%m/%d %H:%M:%S")
    # 控制台处理器指定格式
    sh.setFormatter(formatter)
    # 添加文件处理器到日志器
    logger.addHandler(fh)
    # 文件处理器设置格式
    fh.setFormatter(formatter)
    logging.warning("警告信息")
    logging.error("错误信息")
    logging.critical("严重错误信息")
  • 相关阅读:
    iphone inline video fragments
    input text focus去掉默认光影
    ios html5 audio 不能自动播放
    跑马灯实现新闻滚动 鼠标放上去停 移开继续滚动
    转:理解WinCE bulid过程
    C语言sendto()函数:经socket传送数据
    WaitForSingleObject用法介绍
    CoInitializeEx()
    转:select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
    setsocketopt()
  • 原文地址:https://www.cnblogs.com/Murraya/p/13023875.html
Copyright © 2011-2022 走看看