python logging日志模板
程序员文章站
2022-07-15 15:45:35
...
首先调用python自带的 logging模块
然后直接复制就行
然后日志文件和控制台就都能打印了
# 创建日志
debug_list = {'INFO': logging.INFO, 'DEBUG': logging.DEBUG, 'WARNING': logging.WARNING, 'ERROR': logging.ERROR,'CRITICAL': logging.CRITICAL}
parser = argparse.ArgumentParser()
parser.add_argument('--debug', dest='debug', required=False, default='INFO')
args = parser.parse_args()
debug = args.debug
debug = debug.upper()
# create logger
log_name = datetime.date.isoformat(datetime.datetime.now().date()) + '.log'
logfile = log_path + os.sep + log_name
logging.basicConfig(level=debug_list[debug],format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s',datefmt='%a, %d %b %Y %H:%M:%S',filename=logfile,filemode='a')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
下一篇: Javascript的一些坑点