javascript - how can I log every method call in node.js without adding debug lines everywhere? -
i log user_id of person making request , method name of every method called javascript class. 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 async user 35 , 78 might doing stuff @ same time. want make sure each log line starts user_id can grep , see 1 user's activity @ time.
is there super clever way without adding logger statements every method?
i'm guessing web app, in case if using connect can use logger middleware logs user , url path, sufficient. otherwise, going have metaprogramming along lines of wrapping each function in wrapper function logging.
function logcall(realfunc, instance) { return function() { log.debug('user: ' + instance.user_id + ' method ' + realfunc.name); return realfunc.apply(instance, arguments); }; }
for work, class method's must named functions, not anonymous.
function sendmessage() { //code send message //can use `this` access instance properties } function myclass(userid) { this.userid = userid; //or whatever this.sendmessage = logcall(sendmessage, this); //repeat above line each instance method want instrumented logging }
Comments
Post a Comment