• 在线客服

  • 扫描二维码
    下载博学谷APP

  • 扫描二维码
    关注博学谷微信公众号

  • 意见反馈

原创 Python logging源码学习笔记

发布时间:2020-06-01 15:05:47 浏览 3353 来源:博学谷 作者:照照

    今天我们来梳理一下自动化测试中的Python logging源码学习笔记,主要内容分为三个部分,分别是基本使用、文件存储与日志打印和API。感兴趣的小伙伴赶紧一起来看看吧~

     

    Python logging源码

     

    1、基本使用

     

    import  logging

     

    # logging 日志配置

    logging.basicConfig(filename='exampe.log',level=logging.DEBUG)

     

    logging.debug("helloworld - debug")

    logging.info('hello  info')

    logging.warning('hello warning')

    logging.error('hello error')logging.critical('hello critical')

     

    2、文件存储与日志打印

     

    import logging

     

    # create logger

    logger = logging.getLogger('simple_example')

    logger.setLevel(logging.DEBUG)

     

    # create console handler and set level to debug

    ch = logging.StreamHandler()

    ch.setLevel(logging.DEBUG)

    fl = logging.FileHandler('app.log')

    fl.setLevel(logging.DEBUG)

     

    # create formatter

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

     

    # add formatter to ch

    ch.setFormatter(formatter)

    fl.setFormatter(formatter)

    # add ch to logger

    logger.addHandler(ch)

    logger.addHandler(fl)

     

    # 'application' code

    logger.debug('debug message')

    logger.info('info message')

    logger.warning('warn message')

    logger.error('error message')

    logger.critical('critical message')

     

    3API

     

    import requests

    import logging

     

    logging.basicConfig(format='%(levelname)s %(asctime)s %(message)s',level=logging.DEBUG)

     

    base_url = "http://39.107.96.138:3000/api/v1/"

     

    testdata = {

            "accesstoken":"49b2e830-4305-475d-b6b5-52287

    cc5daaa",

            "title":"2313131231231232",

            "tab":"ask",

            "content":"xxxxxxxxxxxxx"

        }

    def test_new_topic():

        """

        测试发布话题

        :return:

        """

        url = base_url+'topics'

        logging.info(f"开始发送Post请求{url},请求数据为{str(testdata)}")

        r = requests.post(url,json=testdata)

        jsonData = r.json()

        logging.info(f'发送请求完成,结果为{str(jsonData)}')

        assert r.status_code == 200

     

        assert jsonData['success']

        logging.info(f"test_new_topic, topicid: {jsonData['topic_

    id']}")

     

        assert jsonData['topic_id'] is not None

     

        return jsonData['topic_id']

     

    以上就是Python logging源码学习笔记的全部内容,如果想学习更多关于自动化测试的知识点,可以在博学谷官网报名申请相关免费课程的试听~

     

    申请免费试学名额    

在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!

上一篇: 学软件测试哪个学校好? 下一篇: 软件测试中白盒测试的优缺点是什么?

相关推荐 更多

热门文章

  • 前端是什么
  • 前端开发的工作职责
  • 前端开发需要会什么?先掌握这三大核心关键技术
  • 前端开发的工作方向有哪些?
  • 简历加分-4步写出HR想要的简历
  • 程序员如何突击面试?两大招带你拿下面试官
  • 程序员面试技巧
  • 架构师的厉害之处竟然是这……
  • 架构师书籍推荐
  • 懂了这些,才能成为架构师
  • 查看更多

扫描二维码,了解更多信息

博学谷二维码