Skip to content

Logging

Logging configuration and utilities for Lambda handlers.


Logging utilities for AWS Lambda handlers.

Provides logging mixins and helper functions for configuring structured logging with AWS Lambda Powertools.

LoggingMixins

Bases: HandlerMixins

Mixin class providing structured logging capabilities.

Integrates AWS Lambda Powertools Logger for structured JSON logging with automatic context injection and correlation IDs.

Attributes:

Name Type Description
log Logger

Alias for the logger property.

logger Logger

The AWS Lambda Powertools Logger instance.

log property writable

log

Alias for the logger property.

Returns:

Type Description
Logger

The configured Logger instance.

logger property writable

logger

Get the Logger instance, creating one if needed.

Returns:

Type Description
Logger

The configured Logger instance for this handler.

add_logger_to_root

add_logger_to_root()

Add this handler's logger to the root logger.

Ensures log messages from other modules are captured with the same structured format.

Source code in src/aibs_informatics_aws_lambda/common/logging.py
88
89
90
91
92
93
94
def add_logger_to_root(self):
    """Add this handler's logger to the root logger.

    Ensures log messages from other modules are captured with
    the same structured format.
    """
    add_handler_to_logger(self.logger, None)

get_logger classmethod

get_logger(service=None, add_to_root=False)

Create a new Logger instance.

Parameters:

Name Type Description Default
service Optional[str]

The service name for the logger. If None, uses default.

None
add_to_root bool

Whether to add the logger handler to the root logger.

False

Returns:

Type Description
Logger

A configured Logger instance.

Source code in src/aibs_informatics_aws_lambda/common/logging.py
75
76
77
78
79
80
81
82
83
84
85
86
@classmethod
def get_logger(cls, service: Optional[str] = None, add_to_root: bool = False) -> Logger:
    """Create a new Logger instance.

    Args:
        service (Optional[str]): The service name for the logger. If None, uses default.
        add_to_root (bool): Whether to add the logger handler to the root logger.

    Returns:
        A configured Logger instance.
    """
    return get_service_logger(service=service, add_to_root=add_to_root)

add_handler_to_logger

add_handler_to_logger(source_logger, target_logger=None)

Add a source logger's handler to a target logger.

Copies the handler from the source logger to the target logger, ensuring consistent log formatting across loggers.

Parameters:

Name Type Description Default
source_logger Logger

The Logger whose handler will be copied.

required
target_logger Union[str, Logger, None]

The target logger to receive the handler. Can be a logger name string, a Logger instance, or None for the root logger.

None
Source code in src/aibs_informatics_aws_lambda/common/logging.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def add_handler_to_logger(
    source_logger: Logger, target_logger: Union[str, logging.Logger, None] = None
):
    """Add a source logger's handler to a target logger.

    Copies the handler from the source logger to the target logger,
    ensuring consistent log formatting across loggers.

    Args:
        source_logger (Logger): The Logger whose handler will be copied.
        target_logger (Union[str, logging.Logger, None]): The target logger to receive the handler.
            Can be a logger name string, a Logger instance, or None
            for the root logger.
    """
    handler = source_logger.registered_handler

    if target_logger is None or isinstance(target_logger, str):
        target_logger = logging.getLogger(target_logger)
        log_level = min(source_logger.log_level, target_logger.getEffectiveLevel())
        target_logger.setLevel(log_level)
    target_logger_handlers = get_all_handlers(target_logger)

    # TODO: This is not avoiding duplicate handlers.
    # we need to have better comparison logic
    if handler not in target_logger_handlers:
        target_logger.addHandler(handler)

get_service_logger

get_service_logger(
    service=None, child=False, add_to_root=False
)

Create a service logger with optional root logger integration.

Parameters:

Name Type Description Default
service Optional[str]

The service name for the logger. If None, uses default.

None
child bool

Whether to create a child logger.

False
add_to_root bool

Whether to add the logger handler to the root logger.

False

Returns:

Type Description
Logger

A configured Logger instance for the service.

Source code in src/aibs_informatics_aws_lambda/common/logging.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def get_service_logger(
    service: Optional[str] = None, child: bool = False, add_to_root: bool = False
) -> Logger:
    """Create a service logger with optional root logger integration.

    Args:
        service (Optional[str]): The service name for the logger. If None, uses default.
        child (bool): Whether to create a child logger.
        add_to_root (bool): Whether to add the logger handler to the root logger.

    Returns:
        A configured Logger instance for the service.
    """
    service_logger = Logger(service=service, child=child)
    if add_to_root:
        add_handler_to_logger(service_logger)
    return service_logger