squirrel2300

导航

Python logging

拒绝print,使用 logging !

#https://docs.python.org/3.5/howto/logging.html?highlight=logging

https://docs.python.org/3.5/library/logging.html

#win10 + python 3.5.2

1. Logging to Console

import logging

#default level : WARNING  #30

#CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

logging.debug('debug message')

logging.info('info message')

logging.warning('warning message')

 

>>> [log.CRITICAL ,log.ERROR, log.WARNING,log.INFO,log.DEBUG,log.NOTSET]
[50, 40, 30, 20, 10, 0]

>>> log.getLevelName(logger.getEffectiveLevel())
'WARNING'

 

2. Logging to a file

 

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

 

posted on 2017-04-12 00:24  squirrel2300  阅读(111)  评论(0编辑  收藏  举报