logging模块自定义日志

周朗
2023-12-01
import logging


class Log:
    _isinstance = None

    def __new__(cls, *args, **kwargs):
        if cls._isinstance is None:
            cls._isinstance = object.__new__(cls)
        return cls._isinstance

    def __init__(self):
        formatter = logging.Formatter(f"%(asctime)s %(levelname)s: %(message)s")
        sh = logging.StreamHandler()
        sh.setFormatter(formatter)
        _log = logging.getLogger(__name__)
        _log.setLevel(logging.INFO)

        self.loger = _log

        # self.loger = logging.getLogger()

    def info(self, msg):
        self.loger.info(f"\033[0;32m {msg} \033[0m")

    def error(self, msg):
        self.loger.error(f"\034 {msg} \033")

 类似资料: