pygogoを使ってログをjsonで出す。

More than 3 years have passed since last update.

import pygogo as gogo

formatter = gogo.formatters.structured_formatter
logger = gogo.Gogo('struct', low_formatter=formatter).get_logger(foo="barbar")

logger.debug('bought', extra={"type": "apple", "price": 250, "color": "red"})
#=> {"name": "struct.base", "level": "DEBUG", "color": "red", "price": 250, "msecs": 587.5449180603027, "time": "2016-01-25 16:02:24", "message": "bought", "foo": "barbar", "type": "apple"}

logger.debug('bought', extra={"type": "PC", "price": 100000, "memory": "16GB"})
#=>{"name": "struct.base", "level": "DEBUG", "price": 100000, "memory": "16GB", "msecs": 587.9619121551514, "time": "2016-01-25 16:02:24", "message": "bought", "foo": "barbar", "type": "PC"}

コンストラクタの引数にlow_levelを渡して上げると出力制御ができるっぽい。

import pygogo as gogo

formatter = gogo.formatters.structured_formatter
logger = gogo.Gogo('struct', low_formatter=formatter, low_level='info').get_logger(foo="barbar")

logger.debug('bought', extra={"type": "apple", "price": 250, "color": "red"})
#=>出ない
logger.info('bought', extra={"type": "PC", "price": 100000, "memory": "16GB"})
#=>出る