2017-04-24 5 views
-3

デバッグする回数を少なくしてログを記録する方法はありますか? 私は情報、警告、クリティカル、エラー、何か他のものをファイルに書きたいと思います。Pythonのロギングモジュール。すべてのデバッグをログに記録する方法はありますか?

これは、デバッグログも同様に検索することに狂ってしまうためです。

この瞬間に私がやっている:私はすべてを記録する。このように

import logging 
logging.basicConfig(filename='loggs.log', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',level=logging.DEBUG) 
logging.info('Start Test API.') 

を、私はそれを制限したいです!

私はこれをやっているいくつかのドキュメント読んで後:「スタートテストAPI」を

LEVELS = {'info': logging.INFO, 
      'warning': logging.WARNING, 
      'error': logging.ERROR, 
      'critical': logging.CRITICAL} 

#define the loggs configuration 
if len(sys.argv) > 1: 
    level_name = sys.argv[1] 
    level = LEVELS.get(level_name, logging.NOTSET) 
    logging.basicConfig(filename='loggs.log', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',level=level) 
logging.info('Start Test API.') 

ので、私はそれがログ期待していが、代わりに私のファイルは空です!

+2

スタックオーバーフローの質問は1つの特定の質問であることを意味します。あなたの質問に答えられたならば、あなたは答えを受け入れるべきです、そして、新しい質問は別々に尋ねられるべきです。 –

+0

こんにちは@KevinMGrangerはい、これは本当ですが、私はちょうど答えを試して、それは私のために働いていないので、私の質問に完全に正解ではないので、それは働いていない –

+0

どのようにプログラムを呼び出しましたか?最初のパラメータは "info"ですか?そこに 'print("設定レベル "、レベル)'を入れて、それが何を表示しているかを見てください。 – tdelaney

答えて

3

の代わりに:

logging.basicConfig(filename='loggs.log', format='', datefmt='', level=logging.DEBUG) 

あなたが欲しい:https://docs.python.org/3.5/library/logging.html

例:

logging.basicConfig(filename='loggs.log', format='', datefmt='', level=logging.INFO) 

は、公式ドキュメントを参照してください

import logging 
import sys 

LEVELS = {'debug': logging.DEBUG, 
      'info': logging.INFO, 
      'warning': logging.WARNING, 
      'error': logging.ERROR, 
      'critical': logging.CRITICAL} 

if len(sys.argv) > 1: 
    level_name = sys.argv[1] 
    level = LEVELS.get(level_name, logging.NOTSET) 
    logging.basicConfig(level=level) 

logging.debug('This is a debug message') 
logging.info('This is an info message') 
logging.warning('This is a warning message') 
logging.error('This is an error message') 
logging.critical('This is a critical error message') 

が生成されます

$ python logging_level_example.py debug 
DEBUG:root:This is a debug message 
INFO:root:This is an info message 
WARNING:root:This is a warning message 
ERROR:root:This is an error message 
CRITICAL:root:This is a critical error message 

$ python logging_level_example.py info 
INFO:root:This is an info message 
WARNING:root:This is a warning message 
ERROR:root:This is an error message 
CRITICAL:root:This is a critical error message 
+0

ちょっと例を読む... tks so much;) –

+0

INFOと_above_のすべてを記録します。私はドキュメントからの例で私の答えを更新しました。 – alxwrd

+0

2.6版のドキュメントにリンクした理由はありますか?サポートはほぼ4年前に終了しました。 –

0

ログを制限するレベルをsetLevelメソッドを使用して設定できます。

+0

私は例を教えてください。私が "情報"を設定すると、情報だけが設定されますが、ログ情報、エラー、警告とクリティカルが必要です –

関連する問題