Files
huojv/utils/logger.py
2025-10-30 23:39:27 +08:00

27 lines
784 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import sys
import time
from typing import Dict
# 基础logger配置控制台输出
_logger = logging.getLogger("huojv")
if not _logger.handlers:
_logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
fmt = logging.Formatter(fmt='[%(asctime)s] %(levelname)s %(message)s', datefmt='%H:%M:%S')
handler.setFormatter(fmt)
_logger.addHandler(handler)
# 简单的节流打印同一个key在interval秒内只打印一次
_last_log_times: Dict[str, float] = {}
def throttle(key: str, interval_sec: float, level: int, msg: str):
now = time.time()
last = _last_log_times.get(key, 0.0)
if now - last >= interval_sec:
_last_log_times[key] = now
_logger.log(level, msg)
# 对外暴露
logger = _logger