如何使用摩根记录器?

编程入门 行业动态 更新时间:2024-10-28 04:17:18
本文介绍了如何使用摩根记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我无法使用Morgan登录。它不会将信息记录到控制台。文档没有说明如何使用它。

我想看看一个变量是什么。这是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(..));

更多推荐

如何使用摩根记录器?

本文发布于:2023-11-15 17:33:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596533.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:记录器   如何使用   摩根

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!