如何學(xué)習(xí)Pythonlogging源碼

今天就跟大家聊聊有關(guān)如何學(xué)習(xí)Python logging源碼,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)從2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元饒陽(yáng)做網(wǎng)站,已為上家服務(wù),為饒陽(yáng)各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

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、文件存儲(chǔ)與日志打印

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')

3、API

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():

    """

    測(cè)試發(fā)布話題

    :return:

    """

    url = base_url+'topics'

    logging.info(f"開(kāi)始發(fā)送Post請(qǐng)求{url},請(qǐng)求數(shù)據(jù)為{str(testdata)}")

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

    jsonData = r.json()

    logging.info(f'發(fā)送請(qǐng)求完成,結(jié)果為{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']

看完上述內(nèi)容,你們對(duì)如何學(xué)習(xí)Python logging源碼有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站標(biāo)題:如何學(xué)習(xí)Pythonlogging源碼
轉(zhuǎn)載來(lái)源:http://bm7419.com/article24/ijpsce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、微信公眾號(hào)、企業(yè)建站外貿(mào)建站、虛擬主機(jī)、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)