javascript - 如何在 node.js 中记录每个方法调用而不在任何地方添加调试行?

标签 javascript node.js logging coffeescript

我想记录发出请求的人的 user_id 以及为 javascript 类调用的每个方法的方法名称。例如:

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

由于一切都是异步的,用户 35 和 78 可能同时在做一些事情。所以我想确保每个日志行都以他们的 user_id 开头,这样我就可以 grep 并且一次只能看到一个用户的事件。

有没有一种 super 聪明的方法可以在不向每个方法添加记录器语句的情况下做到这一点?

最佳答案

答案基本正确,但这里是如何避免无限递归

Javascript

(function () {
  var oldCall = Function.prototype.call;
  var newCall = function(self) {
    Function.prototype.call = oldCall;
    console.log('Function called:', this.name);
    var args = Array.prototype.slice.call(arguments, 1);
    var res = this.apply(self, args);
    Function.prototype.call = newCall;
    return res
  }
  Function.prototype.call = newCall;
})();

CoffeeScript

do ->
  oldCall = Function::call
  newCall = (self) ->
    Function::call = oldCall
    console.log "Function called: #{this.name}"
    args = Array.prototype.slice.call arguments, 1
    res = this.apply self, args
    Function::call = newCall
    res
  Function::call = newCall

关于javascript - 如何在 node.js 中记录每个方法调用而不在任何地方添加调试行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15889766/

相关文章:

javascript - 如何在 Canvas 中实现英雄选择?

javascript - 在 Javascript 中暂停直到调用回调

javascript - 如何禁用 storybook 的 webpack 的 eslint-loader?

javascript - 安装 lumx Angular Material 设计框架时出错?

javascript - Instagram 新 API,通过标签获取项目

node.js - 什么时候可以使用 async.eachLimit 而不是 async.eachSeries

ios - Google App Engine 和 Socket.IO 上的移动聊天应用程序

java - 关闭 tomcat 日志记录

logging - 如何将自定义日志添加到 Visual Studio Team Services 构建的日志输出?

java - 如何将日志记录与异常处理链结合起来?