zoukankan      html  css  js  c++  java
  • logging-日志信息管理

    1 logging

    1.1 简单介绍

    logging就如它的名字一样,属于日志模块,常用在需要将打印信息输出到文件保存的时候,在自动化测试的脚本中,可以用该模块保存测试的记录数据。

    1.2 快速入门

    Step1:编辑example.py, 内容如下

    import logging
    logging.basicConfig(
        filename="example.log",
        filemode='w',
        level=logging.DEBUG,
        datefmt = '%m-%d %H:%M:%S',
        format = 'TIME %(asctime)s %(levelname)-8s %(message)s',
        )
    logging.debug("This message shold go to the log file")
    logging.info("So should this")
    logging.warning("Add this,too")
    
    import sys
    stream = logging.StreamHandler(stream=sys.stdout)
    stream.setLevel(logging.INFO)
    formatter = logging.Formatter('TIME %(asctime)s : %(levelname)-8s %(message)s')
    stream.setFormatter(formatter)
    logging.getLogger().addHandler(stream)
    
    logging.debug("This message shold go to the log file only")
    logging.info("This will show both the strout and the log file")
    logging.warning("Add this,too")
    

    Step2:执行Run的快捷键,Alt+Shift+F10

     Step3:执行后将会同目录下生成一个example.log文件,文件内容如下

    1 TIME 04-20 17:23:10 DEBUG    This message shold go to the log file
    2 TIME 04-20 17:23:10 INFO     So should this
    3 TIME 04-20 17:23:10 WARNING  Add this,too
    4 TIME 04-20 17:23:10 DEBUG    This message shold go to the log file only
    5 TIME 04-20 17:23:10 INFO     This will show both the strout and the log file
    6 TIME 04-20 17:23:10 WARNING  Add this,too

    1.3 代码解释

    • 使用logging模块时,需要导入先该模块,导入方法 “import  logging”
    • logging模块默认是输出到console,并且默认的level是logging.WARNING,低于该级别的信息就不会被输出。
    • logging级别的高低如下:logging.DEBUG < logging.INFO < logging.WARNING < logging.ERROR < logging.CRITICAL,可以修改日志的级别,这样低于该级别的信息都不会被输出,修改的方法有两种:
    1 logging.basicConfig(level=logging.DEBUG)
    2 logging.disable(logging.DEBUG)

    logging日志的设置用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被忽略。
    • logging模块还有很多其他更强大更复杂的功能,以后如果有机会再深入研究,就目前而言,了解上面这些logging模块的知识已经足够使用了。

    1.4 Reference

  • 相关阅读:
    JavaScript数组去重(12种方法,史上最全转载)
    数组从大到小排序的两种方式
    jquery.inArray()和splice()使用小记
    HbuilderX 线上打包Android9.0版本无法更新
    常见的移动端H5页面开发遇到的坑和解决办法
    监听点击物理返回键及mui.fire父子页面传参
    深入理解DOM事件类型系列第三篇——变动事件
    jquery监听div等元素的内容变化
    寒假进度3
    寒假进度2
  • 原文地址:https://www.cnblogs.com/aimmiao/p/12739261.html
Copyright © 2011-2022 走看看