Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudWatchLogFormatter mutates logged dicts #198

Open
medley56 opened this issue Apr 12, 2024 · 0 comments
Open

CloudWatchLogFormatter mutates logged dicts #198

medley56 opened this issue Apr 12, 2024 · 0 comments

Comments

@medley56
Copy link

medley56 commented Apr 12, 2024

Summary

When using the CloudWatchLogFormatter with its add_log_record_attrs functionality, it mutates dictionary objects that are passed to logging calls.

Reproducible Example

import logging
import watchtower

logger = logging.getLogger()  # root logger
logger.setLevel(logging.DEBUG)
added_attrs = ('lineno', 'funcName', 'levelname', 'created', 'module')
formatter = watchtower.CloudWatchLogFormatter(add_log_record_attrs=added_attrs)

stream = logging.StreamHandler()
stream.setFormatter(formatter)
logger.addHandler(stream)

logged_value = {"key": "value"}
print(logged_value)  # unchanged
logger.info(logged_value)
print(logged_value)  # mutated

Suggested Mitigation

It would be a performance hit but we could deepcopy the dict before adding all the attributes. This is what I'm doing on my custom implementation of a JsonFormatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant