给python加log
转载的,本人用过,用来调试下位机中的python文件,很好用
# -*- coding: utf-8 -*-
import loggingimport math
logger = logging.getLogger()
#set loghandler
file = logging.FileHandler("qqxml.log")
logger.addHandler(file)
#set formater
#formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
file.setFormatter(formatter)
#set log level
logger.setLevel(logging.NOTSET)
for i in range(0,10):
logger.info(str(i))
使用方法如下:
logger.error(message)
logger.info(message)
原著:http://san-yun.iteye.com/blog/1553063
thanks :SAN_YUN