# Python Lambda logging duplication workaround

**URL:** https://forum.serverless.com/t/python-lambda-logging-duplication-workaround/1585
**Category:** Serverless Framework
**Tags:** aws
**Created:** [March 29, 2017, 11:14pm UTC](https://forum.serverless.com/t/python-lambda-logging-duplication-workaround/1585 "2017-03-29T23:14:49Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![boarik](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@boarik](https://forum.serverless.com/u/boarik)
#### Post date: [February 28, 2018, 1:16pm UTC](https://forum.serverless.com/t/python-lambda-logging-duplication-workaround/1585/6 "2018-02-28T13:16:34Z")

</div>

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:

```auto
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"

```

---

_[View the full topic](https://forum.serverless.com/t/python-lambda-logging-duplication-workaround/1585)._
