0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[メモ]テスト時のloggingの出力を無効にしたい

Posted at

自分用のメモ

Djangoを使っていてログを取るためにdjango-structlogを使っている(非常に便利)。
ただテストをするときにも出力が出て邪魔でloggingの出力を抑える方法がないか調べていたらloggingの機能に近いものがあった。別にDjangoに限った話ではないが。

logging.disable(log_level=logging.CRITICAL)

引数にlog_levelを取れ、渡されたlog_level以下のレベルのログを出力しなくなる。こちらドキュメント
Python3.7からはデフォルト引数でCRITICALになった様子。なので引数無しですべてのloggingの出力を抑えられる。
テスト時はコードの開始や、unittestsetUp()メソッド内で実行すればいい。

逆にログ出力をもとに戻すには

logging.disable(log_level=logging.NOTSET)

すればいい。
これでテストの出力を汚染しないですんだ。めでたしめでたし。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?