• <legend id='XswVs'><style id='XswVs'><dir id='XswVs'><q id='XswVs'></q></dir></style></legend>

    <small id='XswVs'></small><noframes id='XswVs'>

    1. <i id='XswVs'><tr id='XswVs'><dt id='XswVs'><q id='XswVs'><span id='XswVs'><b id='XswVs'><form id='XswVs'><ins id='XswVs'></ins><ul id='XswVs'></ul><sub id='XswVs'></sub></form><legend id='XswVs'></legend><bdo id='XswVs'><pre id='XswVs'><center id='XswVs'></center></pre></bdo></b><th id='XswVs'></th></span></q></dt></tr></i><div id='XswVs'><tfoot id='XswVs'></tfoot><dl id='XswVs'><fieldset id='XswVs'></fieldset></dl></div>
      <tfoot id='XswVs'></tfoot>

      • <bdo id='XswVs'></bdo><ul id='XswVs'></ul>

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

        how can I log every method call in node.js without adding debug lines everywhere?(如何在 node.js 中记录每个方法调用而不在任何地方添加调试行?)
        <legend id='7cHio'><style id='7cHio'><dir id='7cHio'><q id='7cHio'></q></dir></style></legend>

          • <i id='7cHio'><tr id='7cHio'><dt id='7cHio'><q id='7cHio'><span id='7cHio'><b id='7cHio'><form id='7cHio'><ins id='7cHio'></ins><ul id='7cHio'></ul><sub id='7cHio'></sub></form><legend id='7cHio'></legend><bdo id='7cHio'><pre id='7cHio'><center id='7cHio'></center></pre></bdo></b><th id='7cHio'></th></span></q></dt></tr></i><div id='7cHio'><tfoot id='7cHio'></tfoot><dl id='7cHio'><fieldset id='7cHio'></fieldset></dl></div>

            <small id='7cHio'></small><noframes id='7cHio'>

            1. <tfoot id='7cHio'></tfoot>
                <bdo id='7cHio'></bdo><ul id='7cHio'></ul>
                    <tbody id='7cHio'></tbody>
                • 本文介绍了如何在 node.js 中记录每个方法调用而不在任何地方添加调试行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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
                  

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

                  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?

                  推荐答案

                  我猜这是一个网络应用程序,在这种情况下,如果您使用连接,您可以使用记录用户和 URL 路径的记录器中间件,这可能就足够了.否则,您将不得不按照将每个函数包装在包装函数中的方式进行一些元编程以进行日志记录.

                  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.

                  function logCall(realFunc, instance) {
                      return function() {
                        log.debug('User: ' + instance.user_id + ' method ' + realFunc.name);
                        return realFunc.apply(instance, arguments);
                      };
                  }
                  

                  为此,您的类方法必须是命名函数,而不是匿名的.

                  For this to work, your class method's must be named functions, not anonymous.

                  function sendMessage() {
                      //code to send message
                      //can use `this` to access instance properties
                  }
                  function MyClass(userId) {
                      this.userId = userId; //or whatever
                      this.sendMessage = logCall(sendMessage, this);
                      //repeat above line for each instance method you want instrumented for logging
                  }
                  

                  这篇关于如何在 node.js 中记录每个方法调用而不在任何地方添加调试行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                  <i id='g6kEp'><tr id='g6kEp'><dt id='g6kEp'><q id='g6kEp'><span id='g6kEp'><b id='g6kEp'><form id='g6kEp'><ins id='g6kEp'></ins><ul id='g6kEp'></ul><sub id='g6kEp'></sub></form><legend id='g6kEp'></legend><bdo id='g6kEp'><pre id='g6kEp'><center id='g6kEp'></center></pre></bdo></b><th id='g6kEp'></th></span></q></dt></tr></i><div id='g6kEp'><tfoot id='g6kEp'></tfoot><dl id='g6kEp'><fieldset id='g6kEp'></fieldset></dl></div>
                • <small id='g6kEp'></small><noframes id='g6kEp'>

                • <legend id='g6kEp'><style id='g6kEp'><dir id='g6kEp'><q id='g6kEp'></q></dir></style></legend>
                        <bdo id='g6kEp'></bdo><ul id='g6kEp'></ul>
                            <tbody id='g6kEp'></tbody>

                            <tfoot id='g6kEp'></tfoot>