您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何學(xué)習(xí)Python logging源碼,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
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ā)布話(huà)題
: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)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。