我想看看一个变量是什么。这是expressjs框架的 response.js 文件的代码:
var logger = require(morgan); res.render = function(view,options,fn){ options = options || {}; var self = this; var req = this.req; var app = req.app; //支持回调函数作为第二个arg if('function'== typeof options){ fn = options,options = {}; } // merge res.locals options._locals = self.locals; //默认回调响应 fn = fn || function(err,str){ if(err)return req.next(err); self.send(str); }; //这里我想看看是什么fn是 //但是它没有显示任何控制台 //如何使用它? logger(fn); // render app.render(view,options,fn); };如何使用摩根?
解决方案看起来你也和我一样的事情感到困惑,我绊倒了这个问题。我认为我们将日志记录与手动日志相关联,就像我们在Java中使用log4j(如果你知道java)在实例化一个记录器的时候一样,并且记录这个''。
然后我在摩根代码中挖掘出来,并不是那种类型的记录器,它用于自动记录请求,响应和相关数据。当作为中间件添加到快速/连接应用程序时,默认情况下应该将语句记录到stdout,显示详细信息:remote ip,请求方法,http版本,响应状态,用户代理等。它允许您使用令牌或通过定义dev或甚至注销输出流,如文件,为它们添加颜色。
为此,我们认为我们可以使用它,如在这里我们还需要使用:
console.log(..);或者,如果要使输出很适合对象:
var util = require(util); console.log(util.inspect(..));
I cannot log with Morgan. It doesn't log info to console. The documentation doesn't tell how to use it.
I want to see what a variable is. This is a code from response.js file of expressjs framework:
var logger = require("morgan"); res.render = function(view, options, fn){ options = options || {}; var self = this; var req = this.req; var app = req.app; // support callback function as second arg if ('function' == typeof options) { fn = options, options = {}; } // merge res.locals options._locals = self.locals; // default callback to respond fn = fn || function(err, str){ if (err) return req.next(err); self.send(str); }; // Here I want to see what fn is // But it doesn't show me anything in console // How to use it? logger(fn); // render app.render(view, options, fn); };How to use Morgan?
解决方案Seems you too are confused with the same thing as I was, the reason I stumbled upon this question. I think we associate logging with manual logging as we would do in Java with log4j (if you know java) where we instantiate a Logger and say log 'this'.
Then I dug in morgan code, turns out it is not that type of a logger, it is for automated logging of requests, responses and related data. When added as a middleware to an express/connect app, by default it should log statements to stdout showing details of: remote ip, request method, http version, response status, user agent etc. It allows you to modify the log using tokens or add color to them by defining 'dev' or even logging out to an output stream, like a file.
For the purpose we thought we can use it, as in this case, we still have to use:
console.log(..);Or if you want to make the output pretty for objects:
var util = require("util"); console.log(util.inspect(..));
更多推荐
如何使用摩根记录器?
发布评论