mjsr
mjsr

Reputation: 7590

Get RequestId in a custom python logger to use in AWS-Lambda

I need to find a mechanism to specify a custom logger without losing the RequestId value provided by default in AWS Lambda.

I was able to modify and customize the properties of the recordlog in the following way:

class PackagePathFilter(logging.Filter):
    def filter(self, record):
        record.relativepath = ''
        if record.pathname is not None:
            record.relativepath = record.pathname.replace('/var/task','')
        return True


logger = logging.getLogger(__name__)
logger.addFilter(PackagePathFilter())
FORMAT = "[%(levelname)s] [%(requestId)s] [%(relativepath)s:%(lineno)d] [%(funcName)s] - %(message)s"
formatter = logging.Formatter(FORMAT)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

The only thing I'm missing from my ideal format is the RequestId.

Upvotes: 1

Views: 867

Answers (2)

Junah201
Junah201

Reputation: 71

Since the default handler's filter injects the aws_request_id into the record, if you remove the default handler, you will not be able to retrieve aws_request_id. Therefore, instead of removing the default handler and adding your own, you should just modify the formatter using setFormatter.

Also you can see the code of default handler and filter https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/849e874de01776cb386c18fb2c1731d43cd2b2f4/awslambdaric/bootstrap.py#L339C1-L342C20

Upvotes: 0

Matthew Pick
Matthew Pick

Reputation: 11

Here is how you can retain the default lambda logger and override it's formatting:

import logging

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

for handler in root.handlers:
    handler.setFormatter(
        logging.Formatter(
            fmt="[%(levelname)s]\t%(asctime)s.%(msecs)03dZ\t%(aws_request_id)s\t%(message)s\n",
            datefmt="%Y-%m-%dT%H:%M:%S",
        )
    )

You get access to the aws_request_id since it is made available via the LambdaLoggerFilter on the default logger.

Reference to AWS Lambda logging bootstrap: https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/2.0.8/awslambdaric/bootstrap.py#L448-L451

If you want a ready-to-go example, here is a gist: https://gist.github.com/matthewpick/3aa01abfeda36eae717837a99994d3ed

Upvotes: 0

Related Questions