• python的logging模块


    Python的logging模块

    一、简单的将日志打印到屏幕

    import logging

    logging.debug('This is debug message')

    logging.info('This is info message')

    logging.warning('This is warning message')

    输出结果为WARNING:root:This is warning message。

    默认情况下,logging将日志打印到屏幕,日志级别为WARNING;日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET,当然也可以自己定义日志级别。

    二、通过logging.basicConfig函数对日志的输出格式及方式做相关配置

    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',
                        filename='myapp.log',
                        filemode='w')
    logging.debug('This is debug message')
    logging.info('This is info message')
    logging.warning('This is warning message')
    结果为在程序所在目录下生成一个名为“myapp.log”的文件,文件内容为:Sun, 29 Oct 2017 18:00:04 test2.py[line:26] DEBUG This is debug message
    Sun, 29 Oct 2017 18:00:04 test2.py[line:27] INFO This is info message
    Sun, 29 Oct 2017 18:00:04 test2.py[line:28] WARNING This is warning message

    如果,再次运行改程序,则文件myapp.log里的内容会被覆盖掉。若想在文件里继续添加内容则需将filemode=‘w’改为filemode=‘a’。

    logging.basicConfig函数各参数:

    filename: 指定日志文件名

    filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'

    format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:

     %(levelno)s: 打印日志级别的数值

     %(levelname)s: 打印日志级别名称

     %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]

     %(filename)s: 打印当前执行程序名

     %(funcName)s: 打印日志的当前函数

     %(lineno)d: 打印日志的当前行号

     %(asctime)s: 打印日志的时间

     %(thread)d: 打印线程ID

     %(threadName)s: 打印线程名称

     %(process)d: 打印进程ID

     %(message)s: 打印日志信息

    datefmt: 指定时间格式,同time.strftime()

    level: 设置日志级别,默认为logging.WARNING

    stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

    三、将日志同时输出到文件和屏幕

    import logging
    logger = logging.getLogger()    #创建一个logger对象
    fh=logging.FileHandler('test.log'#文件输出流对象
    ch=logging.StreamHandler()          #屏幕输出流对象
    formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')    #创建输出格式
    fh.setFormatter(formatter)  #文件输出引用创建的格式
    ch.setFormatter(formatter)  #屏幕输出引用创建的格式
    logger.addHandler(fh)       #进行文件输出
    logger.addHandler(ch)       #进行屏幕输出
    logger.setLevel(logging.DEBUG)  #修改输出日志级别
    logger.debug('This is debug message')
    logger.info('This is info message')
    logger.warning('This is warning message')
    结果为在程序所在目录下生成一个名为“test.log”的文件,文件内容为2017-10-29 18:50:09,717 test2.py[line:29] DEBUG This is debug message
    2017-10-29 18:50:09,717 test2.py[line:30] INFO This is info message
    2017-10-29 18:50:09,718 test2.py[line:31] WARNING This is warning message
    ;屏幕显示2017-10-29 18:50:09,717 test2.py[line:29] DEBUG This is debug message
    2017-10-29 18:50:09,717 test2.py[line:30] INFO This is info message
    2017-10-29 18:50:09,718 test2.py[line:31] WARNING This is warning message
    创建输出格式时,使用的是logging.basicConfig函数各参数:。
    如果不修改输出日志级别,默认是输出WARNING日志级别以上及其本身的日志内容。
    该方法实质思路如下图:

    
    
  • 相关阅读:
    linux开放防火墙端口方法
    Node.js 构建TCP服务
    React高阶组件
    在create-react-app 中启用装饰器语法
    Redux
    微信小程序组件与组件之间的通讯
    微信小程序网络请求
    微信小程序的页面导航跳转
    sqoop2启动job报错
    sqoop2启动client异常
  • 原文地址:https://www.cnblogs.com/xshan/p/7750749.html
Copyright © 2020-2023  润新知