2016-04-20 15 views
2

私はRequestsモジュールを使用してAPIにリクエストを送信し、ansverを受け取って解析し、すべてがOKかどうかを示すPythonスクリプトを用意しています。結果は 'print'と表示されます。ここに簡単な例を示します:Pythonロギングファイルから余分なデータを削除するにはどうしたらいいですか?

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

これらのプリントファイルを記録する必要があります。私はこのようなモジュールロギングを使用する場合:

#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 
+0

indiファイルとコンソールのロギングのための個々のロギングしきい値。次に、メッセージのログレベルを設定します。詳細情報については、次のリンクを参照してください:https://docs.python.org/2/howto/logging-cookbook.html#multiple-handlers-and-formatters – Schore

+0

初期フォーマットにログレベルを追加して、あなたが望むものを見ることができます除外 – user3610360

答えて

1

をしてみてください設定するrequestsロギングレベル

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

それは動作します!どうもありがとうございました!このコードはリクエストモジュールのロギングレバーを設定しますか?私は他のモジュールを角かっこで使うことができますか? –

関連する問題