Python logging - AWS Lambda

0

The code running on AWS Lambda prints the logs properly on Cloudwatch using the logging library.

import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

logger.info(f'Some log')

But, I'm also using a private third party library, and from that I don't get the printed logs.

How does the setup has to be in the library and in the lambda code in order to print the library's logs?

  • please accept the answer if it was useful

1 Answer
1

To ensure that logs from a third-party library are visible in AWS Lambda's CloudWatch logs, you'll need to make sure that the logging configuration within your Lambda function also captures the log output from the third-party library. Here are the steps to configure logging appropriately:

1. Set the Root Logger Level: Since you are setting the logger level for the root logger in your Lambda function, it should inherently apply to all child loggers, unless the third-party library explicitly changes its logger's level or handler.

2. Check Third-Party Library Logging Configuration:

  • Logging Level: Ensure that the third-party library does not set a higher logging level, which could prevent lower-level logs (like DEBUG or INFO) from appearing.
  • Logger Handlers: Sometimes, libraries configure their own logger handlers. If these handlers are not properly set to propagate logs to the root logger, their output might not appear in CloudWatch.

3. Modify Lambda Logging Configuration:

  • Propagate Logs: Make sure the library's logger is set to propagate logs up to the root logger. You can add this configuration to your Lambda function:
# Setup root logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Ensure all child loggers propagate to the root
logging.getLogger('name_of_third_party_library').propagate = True

Replace 'name_of_third_party_library' with the actual logger name used by the third-party library, which typically is the package name.

4. Add Handlers to Root Logger:

  • If you're not seeing any logs from the library, it might be helpful to explicitly add a StreamHandler to the root logger, which ensures all output is printed to standard output and hence, captured by CloudWatch:
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)  # Set to the appropriate level
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)

5. Test the Logging: Before deploying, you might want to test the logging setup locally or in a development environment to make sure that the third-party library's logs are appearing as expected.

6. Review Library Documentation: Check the documentation for the third-party library to see if there are any specific instructions or configurations for logging. Some libraries might have different requirements or configurations that are necessary for logging to work correctly.

profile picture
EXPERT
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions