python的log使用 Posted on 2018-05-08 | Edited on 2018-05-10 12345678910111213141516171819202122232425import logging# create loggerlogger_name = os.path.basename(__file__)logger = logging.getLogger(logger_name)logger.setLevel(logging.DEBUG)# create_filelog_path = os.path.basename(__file__)+".log"fh = logging.FileHandler(log_path)fh.setLevel(logging.DEBUG)#打印到屏幕sh = logging.StreamHandler()sh.setLevel(logging.DEBUG)# create formatterfmt = "[%(asctime)-15s] [%(levelname)s] [%(filename)s - %(lineno)d %(process)d] %(message)s"date_fmt = "%a %d %b %Y %H:%M:%S"formatter = logging.Formatter(fmt, date_fmt)# add handler and formatter to loggerfh.setFormatter(formatter)logger.addHandler(fh)logger.addHandler(sh)logger.debug(sys.version)logger.debug(os.getcwd())