Last active
January 17, 2022 22:16
-
-
Save blade2005/92cd6ffa140ac291214823dfa4e7a337 to your computer and use it in GitHub Desktop.
This file contains 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
import logging | |
def configure_logging(log_context, log_level=logging.INFO, logfmt=None, datefmt=None): | |
"""Configure logging.""" | |
default_logfmt = " ".join( | |
[ | |
"[%(levelname)s]", | |
"%(asctime)s.%(msecs)dZ", | |
"{aws_request_id}", | |
"%(thread)d", | |
"%(message)s", | |
] | |
) | |
default_datefmt = "%Y-%m-%dT%H:%M:%S" | |
if not datefmt: | |
datefmt = default_datefmt | |
if not logfmt: | |
logfmt = default_logfmt | |
logfmt = logfmt.format(**log_context) | |
while logging.root.handlers: | |
logging.root.removeHandler(logging.root.handlers[-1]) | |
logging.basicConfig(level=log_level, datefmt=datefmt, format=logfmt) | |
logging.getLogger("boto3").setLevel(logging.ERROR) | |
logging.getLogger("botocore").setLevel(logging.ERROR) | |
return logging.getLogger() | |
def lambda_handler(event, context): | |
"""Lambda Entrypoint.""" | |
configure_logging( | |
{"aws_request_id": context.aws_request_id}, log_level=logging.INFO | |
) | |
return main(event, context) | |
def main(event, context): | |
logging.info(event) | |
if __name__ == "__main__": | |
configure_logging({"aws_request_id": "local"}, log_level=logging.INFO) | |
print(main(None, None)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment