0

I have the following code, a basic lambda using print & logging.

import json
import logging

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    print("value1 = " + event['key1'])
    print("value2 = " + event['key2'])
    print("value3 = " + event['key3'])
    print("Will it log?")
    
    logger = logging.getLogger()
    logger.debug('This is a debug message')
    logger.info('This is an info message')
    logger.warning('This is a warning message')
    logger.error('This is an error message')
    logger.critical('This is a critical message')
    
    return event['key1']  # Echo back the first key value

I'm trying to get it to log to cloudwatch correctly, but it only outputs when the test values are passed in, none of the other print or log statements are output. If this was a permissions issue I'm assuming this wouldn't print anything to the log? Confused!

Output:

START RequestId: a9aac29d-3e05-4bb0-baad-1e069fe60801 Version: $LATEST
value1 = Hello, Dave
value2 = value2
value3 = value3
END RequestId: a9aac29d-3e05-4bb0-baad-1e069fe60801
REPORT RequestId: a9aac29d-3e05-4bb0-baad-1e069fe60801  Duration: 1.37 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 49 MB  

Test Event:

{
  "key1": "Hello, Dave",
  "key2": "value2",
  "key3": "value3"
}
2
  • 3
    Are you 100% that is the code that generated that output? Maybe you didn't deploy the last changes? Try chaging the prints "print("valueX ..." and verify the output changes Commented Feb 2, 2021 at 19:45
  • You hit the nail on the head with this. For some reason it hadn't deployed. I pushed through another deployment then re-tested and the logging appeared as expected. Thanks all for your help. Commented Feb 3, 2021 at 9:28

1 Answer 1

3
import json
import logging

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

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    print("value1 = " + event['key1'])
    print("value2 = " + event['key2'])
    print("value3 = " + event['key3'])
    print("Will it log?")
    
    logger = logging.getLogger()
    logger.debug('This is a debug message')
    logger.info('This is an info message')
    logger.warning('This is a warning message')
    logger.error('This is an error message')
    logger.critical('This is a critical message')
    
    return event['key1']  # Echo back the first key value

Test input event is as follows; enter image description here

With above code and test input event, logs in AWS CloudWatch are as as follows;

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for validating. Glad it now works :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.