Description
In my code base, all modules have a logger with a well-known service name, which is used across my whole application. This name is passed via the POWERTOOLS_SERVICE_NAME environment variable. While doing so, I'm having duplicated log entries that scales with the number of modules invocation which declare a logger. Example: if my flow passes by 5 modules that declare logger = Logger()
then I will have 5 duplicated entries in my logs.
Expected Behavior
When using logger = GetLogger()
with the same service name, I was expecting to not have logs duplications.
Current Behavior
When using logger = GetLogger()
with the same service name, logs duplication occurs. I believe I already identified the root cause: when a logger is instantiated, even though the underlying logger object is the same, a new handler is being created for the logger, which results in one handler being registered in each logger = GetLogger()
invocation.
Steps to Reproduce (for bugs)
The following snippet will reproduce the issue.
from aws_lambda_powertools import Logger
class Class1:
logger = Logger("class_1")
@staticmethod
def f_1():
Class1.logger.info("log from class_1")
class Class2:
logger = Logger("class_1")
@staticmethod
def f_2():
Class2.logger.info("log from class_2")
Class1.f_1()
Class2.f_2()
{"level": "INFO", "location": "f_2:17", "message": "log from class_2", "timestamp": "2020-12-20 21:58:37,473", "service": "class_1", "sampling_rate": 0.0}
{"level": "INFO", "location": "f_2:17", "message": "log from class_2", "timestamp": "2020-12-20 21:58:37,473", "service": "class_1", "sampling_rate": 0.0}
{"level": "INFO", "location": "f_1:9", "message": "log from class_1", "timestamp": "2020-12-20 21:58:37,476", "service": "class_1", "sampling_rate": 0.0}
{"level": "INFO", "location": "f_1:9", "message": "log from class_1", "timestamp": "2020-12-20 21:58:37,476", "service": "class_1", "sampling_rate": 0.0}
However, if I change the loggers to have different names, no duplication occurs.
I understand I can achieve what I want with the child=True flag, but that leads to some other issues like: how to decide which loggers are child or not.
I feel this is rather a common use case, so I don't understand why there are no reports of this behavior.
What am I missing? Is this by design?
Environment
- Powertools version used: 1.9.0
- AWS Lambda function runtime: Python 3.8
Thanks,
David
Metadata
Metadata
Assignees
Labels
Type
Projects
Status