A better Python logger for AWS Lambda (give me back my “extra” kwarg!)

Luke Rogerson
2 min readOct 31, 2021

Photo by Vidar Smits on Unsplash

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:

  1. 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!
  2. 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!

An example log output in AWS CloudWatch
An example log output in AWS CloudWatch

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Luke Rogerson
Luke Rogerson

Written by Luke Rogerson

Full-stack software engineer. I like to create, teach and learn. www.lukerogerson.me

Responses (1)

Write a response

I just want to say thank you so much!! I feel like this should be in the official AWS documentation, it's so good. I had been trying to get logging the way I wanted for a few weeks and your solution was perfect for me.

--