Pythong中Logging模块基础用法

#日志模块
import logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a,%d %b %Y %H:%M:%S',
                    filename='test.log',
                    filemode='a') #可设置a,w(a保存原先的,w重写覆盖原先的)
logging.debug('debug message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:8] DEBUG debug message
logging.info('info message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:9] INFO info message
logging.warning('warning message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:10] WARNING warning message
logging.error('error message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:11] ERROR error message
logging.critical('critical message')#Fri,17 Nov 2017 16:20:17 logging_moudle.py [line:12] CRITICAL
 critical message

#日志模块
import logging
logger = logging.getLogger() #获取Logger模块
logger.setLevel(logging.DEBUG) #设置Logger级别
fh = logging.FileHandler( 'test2.log') #创建一个handle,用于写于日志文件ch = logging.StreamHandler() #再创建一个handle,用于输出到控制台formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s')#设置格式fh.setFormatter(formatter)ch.setFormatter(formatter)logger.addHandler(fh)logger.addHandler(ch)logger.debug( 'debug message')logger.info( 'info message')logger.warning( 'warning message')logger.error( 'error message')logger.critical( 'critical message')

你可能感兴趣的:(PYTHON)