Python日志器使用方法及原理解析
时间:2020-09-27 17:00:40|栏目:Python代码|点击: 次
日志记录,监控,便于定位bug
进行二次封装
import os import logging from scripts.handle_yaml import do_yaml from scripts.handle_path import LOG_PATH class HandleLog: def __init__(self, name=None): if name is None: self.my_logger = logging.getLogger("testcase") else: self.my_logger = logging.getLogger(name) self.my_logger.setLevel(do_yaml.get_data("log", "logger_level")) console_handler = logging.StreamHandler() console_handler.setLevel("WARNING") log_filename = os.path.join(LOG_PATH, do_yaml.get_data("log", "log_filename")) file_handler = logging.FileHandler(log_filename, encoding="utf-8") formater = logging.Formatter('%(asctime)s - [%(levelname)s] - [msg]: %(message)s - %(name)s - %(lineno)d') console_handler.setFormatter(formater) file_handler.setFormatter(formater) self.my_logger.addHandler(console_handler) self.my_logger.addHandler(file_handler) def get_logger(self): return self.my_logger do_log = HandleLog()
上一篇:python 5个实用的技巧
栏 目:Python代码
本文标题:Python日志器使用方法及原理解析
本文地址:http://www.codeinn.net/misctech/5518.html