标签:tool hand class ret imp 大于 line war evel
---恢复内容开始---
import logging
logging.basicConfig(level=logging.DEBUG, #不配置默认只输出级别大于等于waring的日志
format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘,
datefmt=‘%a, %d %b %Y %H:%M:%S‘,
filename=‘/tmp/test.log‘,
filemode=‘w‘)
logging.debug(‘debug message‘)
logging.info(‘info message‘)
logging.warning(‘warning message‘)
logging.error(‘error message‘)
logging.critical(‘critical message‘)
import logging
def my_logger(filename,file=True,stream=True):
‘‘‘日志函数‘‘‘
logger=logging.getLogger() #日志对象
formatter = logging.Formatter(fmt=‘%(asctime)s -- %(message)s‘,
datefmt=‘%d/%m/%Y %H:%M:%S‘) # 日志格式
logger.setLevel(logging.DEBUG) # 日志级别
if file:
file_handler=logging.FileHandler(filename,encoding=‘utf-8‘) #文件流(文件操作符)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if stream:
stream_handler=logging.StreamHandler() #屏幕流(屏幕操作符)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger
logger=my_logger(‘log‘)
logging.debug(‘出错了‘)
---恢复内容结束---
标签:tool hand class ret imp 大于 line war evel
原文地址:http://www.cnblogs.com/lianxuebin/p/7400166.html