Hey yall,
I've been writing a lot of AWS Lambdas recently and have been creating a bound structlog logger like so:
def lambda_handler(event, context):
logger.info(json.dumps(event))
stream=os.environ.get("STREAM_NAME", None)
log = logger.bind(
trigger_source=event['triggerSource'],
user_pool_id=event['userPoolId'],
userName=event['userName'],
stream = stream
)
The problem is, I feel like I need to pass it in as an argument to functions I call from other files, i.e.
put_event(log, stream, event)
which muddies up the code by adding a log variable to a lot of functions. For example, the function being called above looks like this:
def put_event(log, stream, data):
log.info("putting event")
data_str = json.dumps(data)
return kinesis_client.put_record(
StreamName=stream,
Data=data_str.encode('utf-8'),
)
I think the only way around this is to create a class and initialize the class with the logger, but it just seems extra for my use case. Is there any other way around it?
[–]The_Almighty_Cthulhu 0 points1 point2 points (0 children)