Skip to content

Commit

Permalink
chore: chore
Browse files Browse the repository at this point in the history
  • Loading branch information
cheng zhen committed Feb 13, 2025
1 parent a6096e2 commit 4584657
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,39 @@
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)
Expand Down

0 comments on commit 4584657

Please sign in to comment.