日志等级说明:
UNSET < DEBUG < INFO < WARNNING < ERROR < CRITICAL
import logging logger = logging.getLogger('[test]') #留空则取根日志
logger.setLevel(logging.DEBUG) fh = logging.FileHandler('logfilename.log') #打印至文件
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler() #打印至控制台
ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
fh.setFormatter(formatter)
sh.setFormatter(formatter) logger.addHandler(fh)
logger.addHandler(ch) logger.info('init logger...')