__init__.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from .log_manager import LogManager
  2. import logging
  3. import platform
  4. import os
  5. # 全局日志管理器实例
  6. _log_manager = LogManager()
  7. def get_platform_specific_config_path() -> str:
  8. """根据操作系统自动选择合适的日志配置文件"""
  9. if platform.system() == "Windows":
  10. config_path = "config/logging_config_windows.yaml"
  11. else:
  12. config_path = "config/logging_config.yaml"
  13. # 检查配置文件是否存在,如果不存在则回退到默认配置
  14. if not os.path.exists(config_path):
  15. fallback_path = "config/logging_config.yaml"
  16. if os.path.exists(fallback_path):
  17. return fallback_path
  18. else:
  19. raise FileNotFoundError(f"日志配置文件不存在: {config_path} 和 {fallback_path}")
  20. return config_path
  21. def initialize_logging(config_path: str = None):
  22. """初始化项目日志系统
  23. Args:
  24. config_path: 可选的配置文件路径。如果不提供,将根据操作系统自动选择
  25. """
  26. if config_path is None:
  27. config_path = get_platform_specific_config_path()
  28. _log_manager.initialize(config_path)
  29. def get_logger(name: str, module: str = "default") -> logging.Logger:
  30. """获取logger实例 - 主要API"""
  31. return _log_manager.get_logger(name, module)
  32. # 便捷方法
  33. def get_data_pipeline_logger(name: str) -> logging.Logger:
  34. """获取data_pipeline模块logger"""
  35. return get_logger(name, "data_pipeline")
  36. def get_agent_logger(name: str) -> logging.Logger:
  37. """获取agent模块logger"""
  38. return get_logger(name, "agent")
  39. def get_vanna_logger(name: str) -> logging.Logger:
  40. """获取vanna模块logger"""
  41. return get_logger(name, "vanna")
  42. def get_app_logger(name: str) -> logging.Logger:
  43. """获取app模块logger"""
  44. return get_logger(name, "app")
  45. def get_react_agent_logger(name: str) -> logging.Logger:
  46. """获取react_agent模块logger"""
  47. return get_logger(name, "react_agent")
  48. # 上下文管理便捷方法
  49. def set_log_context(**kwargs):
  50. """设置日志上下文(可选)
  51. 示例: set_log_context(user_id='user123', session_id='sess456')
  52. """
  53. _log_manager.set_context(**kwargs)
  54. def clear_log_context():
  55. """清除日志上下文"""
  56. _log_manager.clear_context()