Skip to Content
強力なPython
book

強力なPython

by Aaron Maxwell
March 2025
Intermediate to advanced
200 pages
2h 51m
Japanese
O'Reilly Media, Inc.
Content preview from 強力なPython

第9章. Pythonでログを取る

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

ロギングは多くのソフトウェアにおいて重要である。 長時間稼動するソフトウェアシステムでは、継続的な遠隔測定とレポーティングを可能にする。また、すべてのソフトウェアにおいて、トラブルシューティングや事後分析のための貴重な情報を提供することができる。 アプリケーションの規模が大きければ大きいほど、ロギングの重要性は増す。しかし、小さなスクリプトでも利点はある。

Pythonはlogging モジュールを通してロギングを提供する。私の意見では、このモジュールはPythonの標準ライブラリの中でも技術的に印象的な部分のひとつだ。よく設計されており、柔軟性があり、スレッドセーフで、強力だ。また、複雑で、動く部分が多く、うまく学ぶのが難しい。この章ではその学習曲線の大部分を克服し、logging が提供する利点の恩恵を十分に受けられるようにする。その価値は十分にあり、何年にもわたって役に立つだろう。

大まかに言って、logging を使うには2つの方法がある。1つは、基本インタフェースと呼び、スクリプト、つまり、1つのファイルに収まるような小さなPythonプログラミングに適している。より本格的なアプリケーションでは、ロガーオブジェクトを使うのが一般的である。これは、より柔軟で、集中制御が可能で、ロギング階層にアクセスできる。重要なアイデアを紹介するために、前者から始める。

基本インターフェース

Pythonのロギング・モジュールを使う最も簡単な方法を紹介しよう:

import logging
logging.warning('Look out!')

これをスクリプトに保存して実行すると、このように出力される:

WARNING:root:Look out!

logging モジュール自体の関数を呼び出すことで、すぐに便利なロギングを行うことができる。logging.warning() を呼び出し、出力行はWARNING で始まっている。logging.error() を呼び出すこともでき、その場合は別の接頭辞が付く:

ERROR:root:Look out!

ログレベル

warning と は異なるerror メッセージログレベルであると言う。 重要度の高い順に、選択できるログレベルがある:1

デバッグ

詳細情報は、通常、、問題を診断するときや開発中にのみ関心がもたれる。

インフォメーション

物事が期待通りに動いていることを確認する。

警告

予期せぬことが起こったことを示す、または近い将来に何らかの問題が起こることを示す(例:「ディスク領域が少ない」)。ソフトウェアは期待通りに動いている。

エラー

より深刻な問題により、ソフトウェアが何らかの関数を実行できなくなっている。

クリティカル

深刻なエラーで、プログラミング自体が実行を継続できない可能性があることを示す

logging.warning()logging.error() のように使い分けるのだ:

logging.debug("Small detail. Useful for troubleshooting.")
logging.info("This is informative.")
logging.warning("This is a warning message.")
logging.error ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AWS上のレジリエントなシステムの構築

AWS上のレジリエントなシステムの構築

Kevin Schwarz, Jennifer Moran, Nate Bachmeier

Publisher Resources

ISBN: 9798341634039