python简单日志工具
创建:2023-10-27 17:52
更新:2025-04-18 21:18
import sys
import datetime

class Log:
    @staticmethod
    def log(color, tag, where, text):
        colorBegin = color
        colorEnd = "\u001b[0m"
        time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        content = f"{colorBegin}[{tag}][{time}][{where.f_code.co_filename}:{where.f_lineno}] {text}{colorEnd}"
        print(content, flush=True)

    @staticmethod
    def debug(text):
        Log.log("", "debug", sys._getframe().f_back, text)
    @staticmethod
    def info(text):
        Log.log("\u001b[32m", "info", sys._getframe().f_back, text)
    @staticmethod
    def warn(text):
        Log.log("\u001b[33m", "warn", sys._getframe().f_back, text)
    @staticmethod
    def error(text):
        Log.log("\u001b[31m", "error", sys._getframe().f_back, text)