LoginSignup
5
6

More than 5 years have passed since last update.

YAML書式で記述した logging の設定ファイルの読み込み

Posted at

本日は

基本 logging チュートリアル

"ロギングの環境設定"においてloggingの設定方法をYAMLで記述したものを読み込むための方法の実装例をみていきませう。

設定ファイルによるもの

チュートリアルから抜粋します。
ただし、datefmt は欠損していたようなので私のほうで捕捉しています。

logging.conf

#logging.conf
[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt='%m/%d/%Y %I:%M:%S %p'

log_conf.py

#log_conf.py
import logging
import logging.config

logging.config.fileConfig('logging.conf')

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

実行例 (on Windows)


> python log_conf.py
'09/26/2017 10:29:59 AM' - simpleExample - DEBUG - debug message
'09/26/2017 10:29:59 AM' - simpleExample - INFO - info message
'09/26/2017 10:29:59 AM' - simpleExample - WARNING - warn message
'09/26/2017 10:29:59 AM' - simpleExample - ERROR - error message
'09/26/2017 10:29:59 AM' - simpleExample - CRITICAL - critical message

YAML から読み込む

チュートリアルから抜粋します。 YAMLは次のように記述できるようです。

tutorial.yaml

version: 1
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
loggers:
  simpleExample:
    level: DEBUG
    handlers: [console]
    propagate: no
root:
  level: DEBUG
  handlers: [console]

問題はこれをどうやって読み込むの?って話です。
Flaskでloggingの設定を外部ファイルから読み込む
を参考に

logging.config.dictConfig(yaml.load(open("config.yaml").read()))

のように記述すればよいみたいです。
ただし、yaml モジュールは外部ライブラリなのでMinicondaであれば次のようにして導入します。

> conda install pyyaml

import するときは

import yaml

と宣言します。

実装例(log_yaml.py)

#log_yaml.py
import logging
import logging.config
import yaml  # conda install pyyaml

def main():
    logging.config.dictConfig(yaml.load(open("tutorial.yaml").read()))
    # create logger
    logger = logging.getLogger('simpleExample')
    # 'application' code
    logger.debug('debug message')
    logger.info('info message')
    logger.warn('warn message')
    logger.error('error message')
    logger.critical('critical message')

if __name__ == '__main__':
    main()

実行例(on Windows)

>python log_conf.py
'09/26/2017 10:41:20 AM' - simpleExample - DEBUG - debug message
'09/26/2017 10:41:20 AM' - simpleExample - INFO - info message
'09/26/2017 10:41:20 AM' - simpleExample - WARNING - warn message
'09/26/2017 10:41:20 AM' - simpleExample - ERROR - error message
'09/26/2017 10:41:20 AM' - simpleExample - CRITICAL - critical message

Reference

基本 logging チュートリアル
Flaskでloggingの設定を外部ファイルから読み込む

5
6
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
5
6