python logging
import logging # # logging 级别 # logging.debug('xxx') # logging.info('xxx') # logging.warning('warnging') # logging.error('error') # logging.critical('critical') # # 用 logging.basicConfig() 设置基本格式 # logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', # level=logging.DEBUG) # logging.debug('debug') # logging.info('info xin xi') # logging.warning('warnging xin xi') # logging.error('error xin xi') # logging.critical('crtical xin xi') # 用 logging.basicConfig() 保存到log文件 # logging.basicConfig(level=logging.DEBUG, # filename='new.log', # filemode='a', ## 写的模式, w,a # format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s', # ) # logging.debug('debug') # logging.info('info') # logging.warning('warning') # logging.error('error') # logging.critical('critical') # 老顽童 # logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') # logger = logging.getLogger(__name__) # # logger.info("info") # logger.debug("debug") # logger.warning('waring') # logger.info("finish") # 输出到文件 # logger = logging.getLogger() # logger.setLevel(level=logging.INFO) # handler = logging.FileHandler("log.txt") # handler.setLevel(logging.INFO) # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # handler.setFormatter(formatter) # logger.addHandler(handler) # # logger.info('iiii') # logger.debug('dddd') # logger.warning('wwww') # logger.info('iiii') # 输出到屏幕和文件 logger = logging.getLogger() logger.setLevel(level = logging.INFO) handler = logging.FileHandler("log.txt") handler.setLevel(logging.INFO) console = logging.StreamHandler() console.setLevel(logging.INFO) logger.addHandler(handler) logger.addHandler(console) logger.info("Start print log") logger.debug("Do something") logger.warning("Something maybe fail.") logger.info("Finish")
参考:
http://www.cnblogs.com/zhbzz2007/p/5943685.html
https://www.cnblogs.com/liujiacai/p/7804848.html