Configure the AgentOps logger with console and optional file handlers. Args: config: Optional Config instance. If not provided, a new Config instance will be created.
(config=None)
| 10 | |
| 11 | |
| 12 | def configure_logging(config=None): # Remove type hint temporarily to avoid circular import |
| 13 | """Configure the AgentOps logger with console and optional file handlers. |
| 14 | |
| 15 | Args: |
| 16 | config: Optional Config instance. If not provided, a new Config instance will be created. |
| 17 | """ |
| 18 | # Defer the Config import to avoid circular dependency |
| 19 | if config is None: |
| 20 | from agentops.config import Config |
| 21 | |
| 22 | config = Config() |
| 23 | |
| 24 | # Use env var as override if present, otherwise use config |
| 25 | log_level_env = os.environ.get("AGENTOPS_LOG_LEVEL", "").upper() |
| 26 | if log_level_env and hasattr(logging, log_level_env): |
| 27 | log_level = getattr(logging, log_level_env) |
| 28 | else: |
| 29 | # Handle string log levels from config |
| 30 | if isinstance(config.log_level, str): |
| 31 | log_level_str = config.log_level.upper() |
| 32 | if hasattr(logging, log_level_str): |
| 33 | log_level = getattr(logging, log_level_str) |
| 34 | else: |
| 35 | log_level = logging.INFO |
| 36 | else: |
| 37 | log_level = config.log_level if isinstance(config.log_level, int) else logging.INFO |
| 38 | |
| 39 | logger.setLevel(log_level) |
| 40 | |
| 41 | # Remove existing handlers |
| 42 | for handler in logger.handlers[:]: |
| 43 | logger.removeHandler(handler) |
| 44 | |
| 45 | # Configure console logging |
| 46 | stream_handler = logging.StreamHandler() |
| 47 | stream_handler.setLevel(log_level) |
| 48 | stream_handler.setFormatter(AgentOpsLogFormatter()) |
| 49 | logger.addHandler(stream_handler) |
| 50 | |
| 51 | # Configure file logging if enabled |
| 52 | log_to_file = os.environ.get("AGENTOPS_LOGGING_TO_FILE", "True").lower() == "true" |
| 53 | if log_to_file: |
| 54 | file_handler = logging.FileHandler("agentops.log", mode="w") |
| 55 | file_handler.setLevel(log_level) |
| 56 | formatter = AgentOpsLogFileFormatter("%(asctime)s - %(levelname)s - %(message)s") |
| 57 | file_handler.setFormatter(formatter) |
| 58 | logger.addHandler(file_handler) |
| 59 | |
| 60 | return logger |
| 61 | |
| 62 | |
| 63 | def intercept_opentelemetry_logging(): |
searching dependent graphs…