2012年8月3日 星期五

Python logging module

import logging

logger = logging.getLogger() # 預設是 root
#logger = logging.getLogger('myapp')

hdlr = logging.FileHandler('/tmp/test.log')

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
hdlr.setFormatter(formatter)

logger.addHandler(hdlr) # 設定 Log 記錄到檔案裡

logger.setLevel(logging.NOTSET) # 設定記錄的等級

logger.debug('teeessst')


* 若 logger.debug 沒有輸出, 可能是記錄層級太高, 需用 Logger.setLevel 設定
 Loggers, Handlers and LogRecords have a severity level. These are the default levels defined in the module - CRITICAL = 50, ERROR = 40, WARNING = 30, INFO = 20, DEBUG = 10, NOTSET = 0. You can create your own or customize them.




Reference :
15.7. logging — Logging facility for Python — Python v2.7.3 documentation
Logging Cookbook — Python v2.7.3 documentation
How Python logging module works | Shut Up and Ship
The python logging module is much better than print statements – t+1
[Python学习]Logging模块的简单使用 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …
惊帆之静默 » Blog Archive » Python的logging模块

沒有留言:

張貼留言