log出力に現在時刻を含める

log出力に現在時刻を追加したかった。

formatterに"%(asctime)s"を渡せば良い。

https://docs.python.org/2.7/library/logging.html#logging.Logger.debug

# -*- coding:utf-8 -*-
import logging
logger = logging.getLogger(__name__)
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
logger.debug('This message should go to the log file')
logger.info('So should this')
logger.warning('And this, too')
view raw xs.py hosted with ❤ by GitHub

出力結果

2014-11-10 09:17:46,154 This message should go to the log file
2014-11-10 09:17:46,154 So should this
2014-11-10 09:17:46,154 And this, too