Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python Logging with AWS Lambda

As the AWS documentation suggests:

import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def my_logging_handler(event, context):     logger.info('got event{}'.format(event))     logger.error('something went wrong') 

Now I made:

import logging logging.basicConfig(level = logging.INFO) logging.info("Hello World!") 

The first snippet of code prints in the Cloud Watch console, but the second one no.

I didn't see any difference as the two snippets are using the root logger.

like image 579
p.magalhaes Avatar asked Jun 08 '16 13:06

p.magalhaes


People also ask

Is Python good for AWS Lambda?

The benefits of Python in AWS Lambda environmentsPython is without a doubt the absolute winner when it comes to spinning up containers. It's about 100 times faster than Java or C#.

How do I add logs to AWS Lambda?

Using the Lambda consoleOpen the Functions page of the Lambda console. Choose a function. Choose Monitor. Choose View logs in CloudWatch.

Can I console log in Lambda?

Using the CloudWatch consoleYou can use the Amazon CloudWatch console to view logs for all Lambda function invocations. Open the Log groups page on the CloudWatch console. Choose the log group for your function (/aws/lambda/ your-function-name ). Choose a log stream.


2 Answers

The reason that logging does not seem to work is because the AWS Lambda Python runtime pre-configures a logging handler that, depending on the version of the runtime selected, might modify the format of the message logged, and might also add some metadata to the record if available. What is not preconfigured though is the log-level. This means that no matter the type of log-message you try to send, it will not actually print.

As AWS documents themselves, to correctly use the logging library in the AWS Lambda context, you only need to set the log-level for the root-logger:

import logging logging.getLogger().setLevel(logging.INFO) 

If you want your Python-script to be both executable on AWS Lambda, but also with your local Python interpreter, you can check whether a handler is configured or not, and fall back to basicConfig (which creates the default stderr-handler) otherwise:

if len(logging.getLogger().handlers) > 0:     # The Lambda environment pre-configures a handler logging to stderr. If a handler is already configured,     # `.basicConfig` does not execute. Thus we set the level directly.     logging.getLogger().setLevel(logging.INFO) else:     logging.basicConfig(level=logging.INFO) 
like image 111
Pit Avatar answered Sep 21 '22 01:09

Pit


Copied straight from the top answer in the question @StevenBohrer's answer links to (this did the trick for me, replacing the last line with my own config):

root = logging.getLogger() if root.handlers:     for handler in root.handlers:         root.removeHandler(handler) logging.basicConfig(format='%(asctime)s %(message)s',level=logging.DEBUG) 
like image 33
Brett Beatty Avatar answered Sep 20 '22 01:09

Brett Beatty