首页 > 代码库 > Python logging

Python logging

拒绝print,使用 logging !

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

#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‘)

 

Python logging