日志的一般用法 log
import logging # logging.warning("warning: error") # logging.critical("112233455") # logging.basicConfig(filename="sample_logging",level=logging.debug("111")) #在debug级别之上的信息才被记录 # logging.basicConfig(format='%(asctime)s %(message)s',datefmt='%d/%m/%Y %h:%M%S') #添加时间格式 # logging.warning("is when this event was logged") logger = logging.getLogger("log_test") # 创建logger,先获取logger对象 logger.setLevel(logging.DEBUG) # 设定一个全局的日志级别 ch = logging.StreamHandler() # 把日志打印到屏幕 ch.setLevel(logging.WARNING) # 给屏幕上的日志设置级别 fh = logging.FileHandler("info.log") fh.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)s -%(levelname)s -%(message)s') ch.setFormatter(formatter) fh.setFormatter(formatter) logger.addHandler(ch) # 告知日志打印到指定的目录里 logger.addHandler(fh) logger.info("info") logger.debug("debug") logger.warning("warning") logger.error("error") logger.critical("critical")