Issue
I'm working on a micro services where i need to log the uuid recieved on a payload to the cloud based logger. But i've one module calling another module, so for every submodule functions to log the messages with uuid, i need to pass uuid to every function i call, but that's a bad idea, is there a better way of doing this ??
Illustration of what i'm trying to achieve:
def call_bar(uuid):
app.logger.info("Something coming with uuid: {uuid}")
def call_foo(uuid):
#
app.logger.info("Something coming with uuid: {uuid}")
call_bar(uuid)
def flask_view():
# i get a uuid here
app.logger.info("Something coming with uuid: {uuid}")
call_foo(uuid)
The issue in the above code is i need to explicitly pass the value to every function i call.
Solution
The logger component of Lambda Power tools does this for us on AWS. the code setup is pretty minimal:
from aws_lambda_powertools import Logger
logger = Logger()
def flask_view():
request_uuid = i_get_a_uuid_here()
logger.append_keys(uuid=request_uuid)
call_foo()
def call_foo()
logger.info("foo happened")
This would get you a json log line than line that as well as bunch of other fields has the following keys.
{
"message": "foo happened", "uuid": "<request_uuid>"
}
if your not on aws lambda, I'd take a look at structlog its log.bind(uuid=uuid)
works similarly allowing you to add keys that get used for subsequent log entries.
Answered By - Nath Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.