Coverage for src / agent_contracts / utils / logging.py: 69%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-09 00:42 +0900

1"""Logging utilities. 

2 

3Provides abstracted logging compatible with standard logging and structlog. 

4""" 

5from __future__ import annotations 

6 

7import logging 

8from typing import Any 

9 

10 

11def get_logger(name: str = "agent_contracts", **context: Any) -> logging.Logger: 

12 """Get a logger instance. 

13  

14 Args: 

15 name: Logger name 

16 **context: Additional context (for structlog compatibility) 

17  

18 Returns: 

19 Logger instance 

20 """ 

21 return logging.getLogger(name) 

22 

23 

24def configure_logging( 

25 level: int = logging.INFO, 

26 format_string: str = "%(levelname)s - %(name)s - %(message)s", 

27) -> None: 

28 """Configure logging. 

29  

30 Args: 

31 level: Logging level 

32 format_string: Log format string 

33 """ 

34 logging.basicConfig(level=level, format=format_string) 

35 

36 

37# Optional structlog integration 

38try: 

39 import structlog 

40 _HAS_STRUCTLOG = True 

41except ImportError: 

42 _HAS_STRUCTLOG = False 

43 

44 

45def get_structured_logger(name: str = "agent_contracts", **context: Any): 

46 """Get a structlog logger if available, otherwise standard logger. 

47  

48 Args: 

49 name: Logger name 

50 **context: Bound context for structlog 

51  

52 Returns: 

53 Logger instance (structlog or standard) 

54 """ 

55 if _HAS_STRUCTLOG: 

56 return structlog.get_logger(name).bind(**context) 

57 return get_logger(name)