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

Viewed 110

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.

0 Answers
Related