簡體   English   中英

如何從 Python 日志文件中刪除額外的數據?

[英]How can I remove extra data from Python logging file?

我有一個 Python 腳本,它使用 Requests 模塊向我們的 API 發送請求,然后接收答案,解析它並說一切正常。 結果顯示為“打印”。 這是一個簡單的例子:

for i in response:
    if i in response_keys:
        print('[Request_name] '+i+' is ok')
    else:
        print('[Request_name] Extra key ' +i+' in resronse)

現在我需要記錄這些打印 ti 文件。 當我像這樣使用 Logging 模塊時:

#Example without sending a request, just logging configuration and check
import logging
logging.basicConfig(filename='Autotests_log.log', level=logging.INFO, format='%(asctime)s %(message)s' )


for i in response:
    if i in response_keys:
        logging.info('[Request_name] '+i+' is ok')
    else:
        logging.info('[Request_name] '+i+' is not ok')

我在日志文件中看到了這一點:

2016-04-20 14:24:44,823 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,873 [Request_name] success is ok
2016-04-20 14:24:44,874 [Request_name] statusCode is ok
2016-04-20 14:24:44,874 [Request_name] data is ok
2016-04-20 14:24:44,876 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,924 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:44,974 Starting new HTTP connection (1): 192.168.1.44
2016-04-20 14:24:45,017 Starting new HTTP connection (1): 192.168.1.44

我應該怎么做才能在日志中看到這個:

2016-04-20 14:24:44,873 [Request_name] success is ok
2016-04-20 14:24:44,874 [Request_name] statusCode is ok
2016-04-20 14:24:44,874 [Request_name] data is ok

嘗試配置requests日志記錄級別

import logging
logging.getLogger("requests").setLevel(logging.WARNING)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM