mardi 22 février 2022

How to design a logging event in python in an efficient way, rather than simply adding the events inside a function?

I want to implement the server-side event analytics feature (using https://segment.com/), I am clear in using the api, just we have to add the event api's inside a function whose action we need to monitor, for example, for creating a new user account in my application, I will have an event inside the function create_user

def create_user(email_id, name, id):
  # some code to add the user in my table
  ....
  ....
  # calling the segment api to track event
  analytics.track(user_id=email_id, event="Add User", properties={"username": name})

The above code works, but design wise I feel it can be done in a better way, since the create_user should have functionality of adding the user only, but here it contains the track event, and I have to do the same in modifying all the areas wherever I need to monitor by adding this analytics api and this makes my code contain irrelevant logic, I read about decorators but my analytics event depend on the code logic inside the function (like if the user email is valid then only I add the user to Db and trigger the event), so that doesn't seem to help.

So I am seeking help here in handling this scenario with the better approach and keeping the code clean. Is there any way or design approach exist for solving this case?

Aucun commentaire:

Enregistrer un commentaire