import loggingclassLogColor:# logging日志格式设置 logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s: %(message)s') @staticmethoddefinfo(message: str):# info级别的日志,绿色 logging.info("\033[0;32m" + message + "\033[0m") @staticmethoddefwarning...
with open(log_path, 'w') as f:f.truncate()f.close()self.logger_format = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s')self.c_logger_format = ColoredFormatter(fmt='%(log_color)s%(asctime)s'' - %(log_color)s%(levelname)s: %(log_color)s%(message)s',reset=...
importloggingimportcolorlogdefget_logger(level=logging.INFO):# 创建logger对象logger = logging.getLogger()logger.setLevel(level)# 创建控制台日志处理器console_handler = logging.StreamHandler()console_handler.setLevel(level)# 定义颜色输出格式color_formatter = colorlog.ColoredFormatter('%(log_color)s%(level...
import logging import sys # 定义颜色代码 class ColoredFormatter(logging.Formatter): def __init__(self, *args, **kwargs): self.color_map = kwargs.pop('color_map', {}) super(ColoredFormatter, self).__init__(*args, **kwargs) def format(self, record): log_msg = super(ColoredFormatte...
1 import logging 2 LOG_LEVEL = logging.NOTSET 3 LOGFORMAT = "[%(log_color)s%(levelname)s] [%(log_color)s%(asctime)s] %(log_color)s%(filename)s [l
self.c_logger_format=ColoredFormatter(fmt='%(log_color)s%(asctime)s' ' - %(log_color)s%(levelname)s: %(log_color)s%(message)s', reset=True, secondary_log_colors={}, style='%' ) self.logger=logging.getLogger(str(random.random())) ...
首先,我们需要导入 Python 的 logging 模块以及其他必要的模块。 importloggingimportsys 1. 2. 步骤2:创建自定义的日志格式 我们可以定义一个带有颜色的日志格式。以下示例定义了不同级别日志的颜色: # 定义颜色常量classLogColors:HEADER='\033[95m'OKBLUE='\033[94m'OKGREEN='\033[92m'WARNING='\033[93m'...
logging基本使用 logging 使用非常简单,使用 basicConfig() 方法就能满足基本的使用需要;如果方法没有传入参数,会根据默认的配置创建Logger 对象,默认的日志级别被设置为 WARNING,该函数可选的参数如下表所示。 参数名称 参数描述 filename 日志输出到文件的文件名 filemode 文件模式,r[+]、w[+]、a[+] format 日志...
colorlog.ColoredFormatter是一个Python logging模块的格式化,用于在终端输出日志的颜色 二. 安装 pip install colorlog AI代码助手复制代码 三. 用法 importcolorloghandler=colorlog.StreamHandler() handler.setFormatter(colorlog.ColoredFormatter('%(log_color)s%(levelname)s:%(name)s:%(message)s')) ...
new_level = logger.level("SNAKY", no= 38, color="<yellow>", icon=" ") logger.log("SNAKY...