Python Lambda logging duplication workaround

Excellent.

Just for future knowledge or anyone else coming across this issue, here is the cause:

Lambda sets up its own form of root logger with a handler attached.
Python’s logging module creates loggers in an hierarchical manner such that each logger is a descendant of the root logger.
With this given, every logger created in the Lambda code will be a descendant of that pre-set root logger.
Python loggers also behave such that a logger would first handle the log message with its attached log handlers, and then propagate the message up the logging hierarchy to its parent. In turn each ancestor up the chain would also issue the message to its handlers and then pass the call up to its parent and so on.

The solution for this can be either deleting the root logger’s handlers altogether
OR
setting the logger’s propagate attribute to False, such that it won’t propagate the log message to its parent and prevent the ancestor’s handlers from being called.

That is:

import logging
import sys

logger = logging.getLogger("my_module")
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("(%(name)s) %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.propagate = False

def lambda_handler(event, context):
    logger.warn("Hello from Lambda")
    
    return "Hello from Lambda"
1 Like