Forked from niranjv/change_lambda_logger_format.py
Last active
November 30, 2017 00:01
-
-
Save Tejadogiparthi/b04bc323ac6fb15959d65eef0f20a5ac to your computer and use it in GitHub Desktop.
Change Python logger format in AWS Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Python logger in AWS Lambda has a preset format. To change the format of the logging statement, | |
| # remove the logging handler & add a new handler with the required format | |
| import logging | |
| import sys | |
| def setup_logging(): | |
| logger = logging.getLogger() | |
| for h in logger.handlers: | |
| logger.removeHandler(h) | |
| h = logging.StreamHandler() | |
| # use whatever format you want here | |
| log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
| h.setFormatter(logging.Formatter(log_format)) | |
| logger.addHandler(h) | |
| logger.setLevel(logging.INFO) | |
| return logger | |
| def lambda_handler(event, context): | |
| logger = setup_logging() | |
| logger.info("This is a test log info statement!") | |
| logger.warning("This is a test log warning statement!") | |
| logger.error("This is a test log error statement") | |
| logger.critical("This is a test log critical error statement") | |
| return | |
| # START RequestId: b8f58da0-d55b-11e7-ba6e-e5de6a0dbbc1 Version: $LATEST | |
| # 2017-11-29 23:19:18,061 - root - INFO - This is a test log info statement! | |
| # 2017-11-29 23:19:18,061 - root - WARNING - This is a test log warning statement! | |
| # 2017-11-29 23:19:18,061 - root - ERROR - This is a test log error statement | |
| # 2017-11-29 23:19:18,061 - root - CRITICAL - This is a test log critical error statement | |
| # END RequestId: b8f58da0-d55b-11e7-ba6e-e5de6a0dbbc1 | |
| # REPORT RequestId: b8f58da0-d55b-11e7-ba6e-e5de6a0dbbc1 Duration: 11.67 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment