LoginSignup
0
3

More than 3 years have passed since last update.

pythonのログ出力(python3)

Last updated at Posted at 2019-08-21

目的

python3でのログ出力をまとめる。

ソース

ログレベルERRORの例

logTest.py
import logging

# ログオブジェクト作成
def get_logger(workername, logLvel):
    # ログの出力名を設定
    logger = logging.getLogger(workername)
    # ログレベルの設定
    logger.setLevel(logLvel)

    return logger

def main():
    # ロガー
    logger = get_logger(__name__, logging.ERROR)
    logger.debug('デバッグログです')
    logger.info('インフォログです')
    logger.warning('ワーニングです')
    logger.error('エラーです')
    logger.critical('クリティカルです')

if __name__ == "__main__":
    main()

上記例のログレベルであるlogging.ERRORは、int型。
実際の値は下記。

ログレベル
DEBUG 10
INFO 20
WARNING 30
ERROR 40
CRITICAL 50

参考

python3のloggingのAPI
https://docs.python.org/ja/3/library/logging.html#levels

最後に

ログレベルは、外部ファイルや環境変数を読み込んで設定するのが普通なのかと思います。

0
3
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
3