morgan logger
morgan is a great logging tool that anyone who works with HTTP servers in Node.js should learn to use. morgan is a middleware that allows us to easily log requests, errors, and more to the console. It’s easy to use, but still powerful and customizable.
morgan是一个很棒的日志记录工具,任何在Node.js中使用HTTP服务器的人都应该学习使用。 morgan是一种中间件,使我们可以轻松地将请求,错误等记录到控制台。 它易于使用,但功能强大且可自定义。
We need to add morgan
to our project’s dependencies. We’ll be using npm
, but feel free to use Yarn. This assumes you’ve already initialized an npm project in your working directory.
我们需要将morgan
添加到项目的依赖项中。 我们将使用npm
,但请随时使用Yarn 。 假设您已经在工作目录中初始化了一个npm项目。
$ npm install morgan --save
Now that we’ve added morgan
to our project, let’s require it.
现在,我们已经将morgan
添加到我们的项目中,让我们对其进行需求。
const morgan = require('morgan');
For simplicity’s sake, our example is going to use an Express server, but morgan
also works with the built-in Node.js http
module.
为简单起见,我们的示例将使用Express服务器 ,但是morgan
也可以与内置的Node.js http
模块一起使用。
const express = require('express');
const morgan = require('morgan');
const app = express();
app.listen(3000, () => {
console.debug('App listening on :3000');
});
Great, our Express server is setup, now let’s add some request logging!
太好了,我们的Express服务器已经设置好了,现在让我们添加一些请求日志!
morgan comes with a suite of presets, which are plug-and-play. To use these, we do this: morgan('tiny')
In this case, tiny
is the name of the predefined format string that we’re using.
morgan附带了一套即插即用的预设。 要使用它们,我们需要这样做: morgan('tiny')
在这种情况下, tiny
是我们正在使用的预定义格式字符串的名称。
Since morgan is a middleware, using it with something like Express is really easy. All we have to do is this:
由于morgan是中间件,因此将它与Express等东西一起使用真的很容易。 我们要做的就是:
const app = express();
app.use(morgan(/* format string here */));
The template strings that morgan uses are called format strings. A format string might look something like this.
morgan使用的模板字符串称为格式字符串。 格式字符串可能看起来像这样。
:method :url :status :res[content-length] - :response-time ms
Tokens are the keywords following the :
’s. Tokens are defined as functions. We’ll look at making custom tokens next.
标记是:
后面的关键字。 令牌被定义为功能。 接下来,我们将介绍制作自定义令牌。
One of the great things morgan allows us to do is make our own tokens. It’s incredibly useful, versatile and easy to do. All we have to do is call morgan.token(name, function)
.
摩根允许我们做的一件伟大的事情就是制作我们自己的代币。 它非常有用,用途广泛且易于执行。 我们要做的就是调用morgan.token(name, function)
。
The first argument we pass-in is the name of the token. If I do morgan.token('my-token', ...);
, for example, I would call that token using :my-token
.
我们传入的第一个参数是令牌的名称。 如果我做morgan.token('my-token', ...);
,例如,我将使用:my-token
来调用该:my-token
。
The second argument is a callback function. morgan will run this each time it logs something using the token. morgan will pass two parameters to the function: req
and res
. Let’s create a token that displays the domain that the request was sent through.
第二个参数是回调函数。 每次它使用令牌记录某些内容时,morgan都会运行此命令。 morgan将向函数传递两个参数: req
和res
。 让我们创建一个令牌,该令牌显示发送请求的域。
morgan.token('host', function(req, res) {
return req.hostname;
});
Using the token we just made is easy. Just call the token like we would any other token.
使用我们刚刚制作的令牌很容易。 就像调用其他令牌一样,只需调用令牌即可。
morgan(':method :host :status :res[content-length] - :response-time ms'); // This is a modified version of morgan's tiny predefined format string.
You’ve probably noticed that brackets ([ ]
) have showed up a few times in our format strings following tokens. Square brackets denote arguments to be passed to a token. We can make our tokens accepts additional arguments. Let’s create a token that grabs one of the parameters from the request.
您可能已经注意到,括号( [ ]
)在标记后面的格式字符串中出现了几次。 方括号表示要传递给令牌的参数。 我们可以使令牌接受其他参数。 让我们创建一个从请求中获取参数之一的令牌。
morgan.token('param', function(req, res, param) {
return req.params[param];
});
And to use it:
并使用它:
morgan(':method :host :status :param[id] :res[content-length] - :response-time ms');
Using morgan with the Node.js built-in http
module is easy as well. Here’s an example from the morgan docs.
使用Node.js内置http
模块的morgan也很容易。 这是来自morgan docs的示例 。
We’re done, yay! That’s all we’re going to cover in this article, but I recommend you checkout the morgan documentation. Stay tuned, we might have more morgan articles coming soon too! Thanks for reading!
我们完成了,是的! 这就是我们将在本文中介绍的全部内容,但是我建议您查阅morgan文档 。 请继续关注,我们可能还会很快发布更多摩根文章! 谢谢阅读!
翻译自: https://www.digitalocean.com/community/tutorials/nodejs-getting-started-morgan
morgan logger