Spaces:
Sleeping
Sleeping
Download structured_logger.py from chaofenghui/cydd: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/chaofenghui/cydd/resolve/main/structured_logger.py
- Command line
-
hf download hf://spaces/chaofenghui/cydd/structured_logger.py
-
curl -L -o structured_logger.py https://huggingface.co/spaces/chaofenghui/cydd/resolve/main/structured_logger.py
1.49 kB
| """ | |
| 结构化日志模块 | |
| 提供统一的日志接口,支持 JSON 格式输出和上下文信息 | |
| """ | |
| import json | |
| import sys | |
| import logging | |
| class StructuredLogger: | |
| """结构化日志包装器""" | |
| _logger = None | |
| def get_logger(cls, name='app'): | |
| if cls._logger is None: | |
| cls._logger = logging.getLogger(name) | |
| return cls._logger | |
| def configure(cls, config): | |
| """根据配置初始化日志系统""" | |
| logger = logging.getLogger() | |
| logger.setLevel(config.LOG_LEVEL) | |
| handler = logging.StreamHandler(sys.stdout) | |
| handler.setFormatter(logging.Formatter(config.LOG_FORMAT)) | |
| logger.handlers.clear() | |
| logger.addHandler(handler) | |
| cls._logger = logging.getLogger('app') | |
| def info(cls, msg, **kwargs): | |
| cls.get_logger().info(f"{msg} {json.dumps(kwargs, ensure_ascii=False, default=str)}" if kwargs else msg) | |
| def warning(cls, msg, **kwargs): | |
| cls.get_logger().warning(f"{msg} {json.dumps(kwargs, ensure_ascii=False, default=str)}" if kwargs else msg) | |
| def error(cls, msg, **kwargs): | |
| cls.get_logger().error(f"{msg} {json.dumps(kwargs, ensure_ascii=False, default=str)}" if kwargs else msg) | |
| def debug(cls, msg, **kwargs): | |
| cls.get_logger().debug(f"{msg} {json.dumps(kwargs, ensure_ascii=False, default=str)}" if kwargs else msg) | |
| log = StructuredLogger |