当前位置:主页 > 软件编程 > Python代码 >

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()

您可能感兴趣的文章:

相关文章