chore: chore
This commit is contained in:
27
logger.py
27
logger.py
@@ -7,18 +7,39 @@ log_dir = "logs"
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
|
||||
|
||||
class PrefixFormatter(logging.Formatter):
|
||||
"""自定义格式化器,为 DEBUG 级别日志添加开源项目前缀"""
|
||||
|
||||
def format(self, record):
|
||||
if record.levelno == logging.DEBUG: # 只给 DEBUG 级别添加前缀
|
||||
record.msg = f"[开源项目:https://github.com/chengazhen/cursor-auto-free] {record.msg}"
|
||||
return super().format(record)
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
filename=os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log"),
|
||||
level=logging.DEBUG,
|
||||
format="%(asctime)s - %(levelname)s - %(message)s",
|
||||
encoding="utf-8",
|
||||
handlers=[
|
||||
logging.FileHandler(
|
||||
os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log"),
|
||||
encoding="utf-8",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
# 为文件处理器设置自定义格式化器
|
||||
for handler in logging.getLogger().handlers:
|
||||
if isinstance(handler, logging.FileHandler):
|
||||
handler.setFormatter(
|
||||
PrefixFormatter("%(asctime)s - %(levelname)s - %(message)s")
|
||||
)
|
||||
|
||||
|
||||
# 创建控制台处理器
|
||||
console_handler = logging.StreamHandler()
|
||||
console_handler.setLevel(logging.INFO)
|
||||
console_handler.setFormatter(logging.Formatter("%(message)s"))
|
||||
console_handler.setFormatter(PrefixFormatter("%(message)s"))
|
||||
|
||||
# 将控制台处理器添加到日志记录器
|
||||
logging.getLogger().addHandler(console_handler)
|
||||
|
||||
Reference in New Issue
Block a user