Logging¶
Simple logging utilities for Arshai framework with OpenTelemetry integration.
This module provides logging support similar to Taloan project - simple and clean, with optional OpenTelemetry integration when available.
Classes¶
- class arshai.utils.logging.ApiEvent(direction, endpoint, message, status=None)[source]¶
Bases:
StructuredLogEventEvent for API call logs
- class arshai.utils.logging.ConnectionEvent(state, connection_type, entity_id, message)[source]¶
Bases:
StructuredLogEventEvent for connection/disconnection logs
- class arshai.utils.logging.ErrorEvent(error_type, message, exception=None)[source]¶
Bases:
StructuredLogEventEvent for error logs
- class arshai.utils.logging.HandlerInterface(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol defining the handler interface
- __init__(*args, **kwargs)¶
- class arshai.utils.logging.InterceptHandler(level=0)[source]¶
Bases:
HandlerHandler to intercept standard library logging into loguru
- class arshai.utils.logging.LogLevel(value)[source]¶
-
Enum representing valid log levels
- TRACE = 'TRACE'¶
- DEBUG = 'DEBUG'¶
- INFO = 'INFO'¶
- SUCCESS = 'SUCCESS'¶
- WARNING = 'WARNING'¶
- ERROR = 'ERROR'¶
- CRITICAL = 'CRITICAL'¶
- class arshai.utils.logging.LoggerAdapter(logger_instance=<loguru.logger handlers=[(id=1, level=20, sink=<stdout>)]>)[source]¶
Bases:
objectAdapter for loguru logger implementing LoggerInterface
- class arshai.utils.logging.LoggerFactory(adapter=None, handler=None)[source]¶
Bases:
objectFactory for creating logger instances
- create_correlation_logger(name, correlation_id=None, trace_id=None, span_id=None)[source]¶
Create a logger with correlation ID and trace context
- Return type:
- class arshai.utils.logging.LoggerInterface(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol defining the logger interface
- __init__(*args, **kwargs)¶
- class arshai.utils.logging.LoggingConfig[source]¶
Bases:
objectConfiguration container for logger settings
- ENV_LOG_LEVEL = 'LOG_LEVEL'¶
- ENV_LOG_FORMAT = 'LOG_FORMAT'¶
- ENV_LOG_JSON = 'LOG_JSON'¶
- DEFAULT_LOG_LEVEL = 'INFO'¶
- DEFAULT_LOG_FORMAT = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{extra[name]}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>'¶
- DEFAULT_LOG_JSON = False¶
- class arshai.utils.logging.LoggingFormat(value)[source]¶
-
Enum representing logging formats
- DEFAULT = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{extra[name]}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>'¶
- JSON = '{message}'¶
- CORRELATION = '<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{extra[name]}</cyan> | <yellow>trace_id={extra[trace_id]}</yellow> | <yellow>span_id={extra[span_id]}</yellow> | <yellow>correlation_id={extra[correlation_id]}</yellow> - <level>{message}</level>'¶
- JSON_CORRELATION = '{message}'¶
- class arshai.utils.logging.LoggingService(handler=None)[source]¶
Bases:
objectCentral service for managing logging configuration
- class arshai.utils.logging.LoguruHandler(logger_instance=<loguru.logger handlers=[(id=1, level=20, sink=<stdout>)]>)[source]¶
Bases:
objectHandler for loguru logger implementing HandlerInterface
- class arshai.utils.logging.MetricEvent(metric_name, value, unit='')[source]¶
Bases:
StructuredLogEventEvent for metric logs
Functions¶
- arshai.utils.logging.configure_logging(level=None, **kwargs)[source]¶
Configure logging for the application (legacy compatibility).
- Parameters:
level (
str) – Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)**kwargs – Other arguments (ignored for compatibility)
- arshai.utils.logging.get_correlation_logger_from_trace(name, current_span=None)[source]¶
Create a logger with correlation ID extracted from the current OpenTelemetry span. If no current span exists, creates a regular logger.
- Parameters:
name (
str) – The logger namecurrent_span – Optional current span to extract context from
- Returns:
A loguru logger with trace context bound
- arshai.utils.logging.get_logger(name)[source]¶
Get a named logger instance with proper context
- Parameters:
name (
str) – The logger name, typically __name__ from calling module- Returns:
A loguru logger instance bound with context data
- arshai.utils.logging.get_logger_with_correlation(name, correlation_id=None, trace_id=None, span_id=None)[source]¶
Get a logger with correlation ID and trace context bound to the context. This is ideal for distributed tracing scenarios where logs need to be correlated across services.
- arshai.utils.logging.get_trace_logger(name, current_span=None)[source]¶
Get a logger with OpenTelemetry trace context
- arshai.utils.logging.log_api(logger_instance, direction, endpoint, status=None, message='', extra_data=None, duration_ms=None)[source]¶
Log API calls consistently
- Parameters:
logger_instance – The logger instance to use
direction (
str) – API direction (‘request’, ‘response’)endpoint (
str) – API endpoint pathstatus (
Optional[int]) – Optional HTTP status code (for responses)message (
str) – Human-readable messageextra_data (
Optional[Dict[str,Any]]) – Optional dictionary with additional context dataduration_ms (
Optional[float]) – Optional duration of the API call in milliseconds
- arshai.utils.logging.log_connection(logger_instance, state, connection_type, entity_id, message, extra_data=None)[source]¶
Log connection/disconnection events consistently
- Parameters:
logger_instance – The logger instance to use
state (
str) – Connection state (‘connected’, ‘disconnected’, etc.)connection_type (
str) – Type of connection (‘sse’, ‘websocket’, ‘redis’, etc.)entity_id (
str) – ID of the connected entity (session ID, user ID, etc.)message (
str) – Human-readable messageextra_data (
Optional[Dict[str,Any]]) – Optional dictionary with additional context data
- arshai.utils.logging.log_error(logger_instance, error_type, message, exception=None, extra_data=None)[source]¶
Log errors consistently
- arshai.utils.logging.log_event(logger_instance, level, event_type, message, data=None)[source]¶
Log an event with consistent structure and optional context data
- Parameters:
logger_instance – The logger instance to use
level (
str) – The log level (debug, info, warning, error, critical)event_type (
str) – Type of event (e.g., ‘connection’, ‘redis’, ‘api_call’)message (
str) – Human-readable messagedata (
Optional[Dict[str,Any]]) – Optional dictionary with additional context data
- arshai.utils.logging.log_metric(logger_instance, metric_name, value, unit='', context=None)[source]¶
Log metrics in a consistent format. This is complementary to OTEL metrics but useful for ad-hoc metrics that don’t need formal instrumentation.