I would like to log the user_id of the person making a request and the method name of every method called for a javascript class. For example:
35 - log_in
35 - list_of_other_users
78 - log_in
35 - send_message_to_user
35 - connect_to_redis
78 - list_of_other_users
Since everything is async user 35 and 78 might be doing stuff at the same time. So I want to make sure each log line starts with their user_id so I can grep for it and only see one user's activity at a time.
Is there a super clever way to do this without adding logger statements to every method?
This is one alternative, not entirely sure how reliable it is though, it feels a bit wrong:
As you can see, it overwrites the
call
function - this creates a slight problem when you try to callconsole.log()
so you need to swap the function back. But it seems to work!EDIT
Since this is tagged CoffeeScript:
I'm guessing this is a web app, in which case if you are using connect you can use a logger middleware that logs the user and the URL path, which is probably sufficient. Otherwise, you are going to have to do some metaprogramming along the lines of wrapping each function in a wrapper function to do the logging.
For this to work, your class method's must be named functions, not anonymous.
The answer is essentially correct, but here's how to avoid infinite recursion
Javascript
Coffeescript