PamGram/utils/log/__init__.py

45 lines
1.2 KiB
Python
Raw Normal View History

2022-10-23 09:15:09 +00:00
import re
from functools import lru_cache
from typing import TYPE_CHECKING
from core.config import config
from utils.log._config import LoggerConfig
2022-10-28 07:11:14 +00:00
from utils.log._logger import (
LogFilter,
Logger,
)
2022-10-23 09:15:09 +00:00
if TYPE_CHECKING:
from logging import LogRecord
__all__ = ["logger"]
logger = Logger(
LoggerConfig(
name=config.logger.name,
width=config.logger.width,
time_format=config.logger.time_format,
traceback_max_frames=config.logger.traceback_max_frames,
log_path=config.logger.path,
keywords=config.logger.render_keywords,
traceback_locals_max_depth=config.logger.locals_max_depth,
traceback_locals_max_length=config.logger.locals_max_length,
2022-10-28 07:11:14 +00:00
traceback_locals_max_string=config.logger.locals_max_string,
2022-10-23 09:15:09 +00:00
)
)
@lru_cache
def _name_filter(record_name: str) -> bool:
2022-12-25 13:26:08 +00:00
return any(re.match(rf"^{name}.*?$", record_name) for name in config.logger.filtered_names + [config.logger.name])
2022-10-23 09:15:09 +00:00
def name_filter(record: "LogRecord") -> bool:
"""默认的过滤器"""
return _name_filter(record.name)
log_filter = LogFilter()
log_filter.add_filter(name_filter)
logger.addFilter(log_filter)