A better Python logger for AWS Lambda (give me back my “extra” kwarg!)
I think that the default AWS Lambda function logging in Python is not particularly readable, making things like using logs for debugging more challenging.
My two main issues are:
- I want to see the logged message first. Why is the
RequestId
the first thing displayed? I don’t want to have to expand/click into a log to read the message! - The
extra
field isn't shown at all. As a Python dev, you've likely made use of this kwarg to pass further useful key/value attributes with a log. I want to also do the same in my Python Lambda functions!
Presented below is my solution to these problems! It works by sub-classing the Logger
class and overriding the makeRecord
method on it. Also surfaced are other helpful attributes such as the module name where the log originated.
Feel free to borrow and make use of this in your own Lambdas!
Usage
logger = use_logging()
logger.info('An informative log message', extra={'user_id': 'abc123', **function_args})
Simply instantiate use_logging
inside of your Lambda function, then the API for logs is the same as what you are useful with the regular Python logger!
