zoukankan      html  css  js  c++  java
  • 2019年6月9日 logging 日志模块

    logging

    logging最多分5个安全等级

    import logging
    
    logging.basicConfig(#参数设定,将级别设定为debug
        level=logging.DEBUG,
        filename='logger.log',#默认将log信息显示在屏幕上,可以自定义路径
        filemode='w',#将模式更改为w,原来是追加
        format="%(asctime)s [%(lineno)d] %(message)s %(filename)s"# 显示当前时间,和 行号,和 反馈信息, 和文件名
    )
    
    logging.debug('debug')
    logging.info('info')
    logging.warning('warning')#默认将warning以上级别才打印出来
    logging.error('error')
    logging.critical('critical')

    》》

    2019-06-10 20:24:44,778 [1505] debug hello.py
    2019-06-10 20:24:44,778 [1506] info hello.py
    2019-06-10 20:24:44,778 [1507] warning hello.py
    2019-06-10 20:24:44,779 [1508] error hello.py
    2019-06-10 20:24:44,779 [1509] critical hello.py

    常用的logger 方法

    logger=logging.getLogger()
    
    fh=logging.FileHandler('FileHander_Log.log') #向文件发送内容,不能向屏幕发送内容
    ch=logging.StreamHandler() #向屏幕发送内容
    
    fm=logging.Formatter('%(asctime)s %(message)s')#定义log格式
    fh.setFormatter(fm)
    ch.setFormatter(fm)
    #将fh ch 设置为fm格式
    
    logger.addHandler(fh)
    logger.addHandler(ch)
    #logger 对象就能又向文件发送内容,又向屏幕发送内容
    logger.setLevel('DEBUG')
    #可以将上面的定义为logger 函数 下面是调用
    
    logger.debug('debug')
    logger.info('info')
    logger.warning('warning')
    logger.error('error')
    logger.critical('critical')

    》》》

    2019-06-10 21:04:44,915 debug
    2019-06-10 21:04:44,915 info
    2019-06-10 21:04:44,915 warning
    2019-06-10 21:04:44,915 error
    2019-06-10 21:04:44,915 critical

     
  • 相关阅读:
    linux执行命令并获取结果(system)
    awk----基本用法
    shell用法总结
    autoit(au3)使用说明
    博客搜集
    vi常用快捷键总结
    python简单学------------程序传参数,列表推导式,set、list、tuple 转换
    python简单学------------模块
    python简单学------------python面向对象(3)
    python简单学------------python面向对象(2)
  • 原文地址:https://www.cnblogs.com/python1988/p/10994297.html
Copyright © 2011-2022 走看看