博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
morgan logger_morgan入门:Node.js Logger中间件
阅读量:2508 次
发布时间:2019-05-11

本文共 4542 字,大约阅读时间需要 15 分钟。

morgan logger

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.

是一个很棒的日志记录工具,任何在Node.js中使用HTTP服务器的人都应该学习使用。 morgan是一种中间件,使我们可以轻松地将请求,错误等记录到控制台。 它易于使用,但功能强大且可自定义。

设定 (Getting Set Up)

We need to add morgan to our project’s dependencies. We’ll be using npm, but feel free to . This assumes you’ve already initialized an npm project in your working directory.

我们需要将morgan添加到项目的依赖项中。 我们将使用npm ,但请随时 。 假设您已经在工作目录中初始化了一个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 , but morgan also works with the built-in Node.js http module.

为简单起见,我们的示例将使用 ,但是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服务器已经设置好了,现在让我们添加一些请求日志!

预定义格式字符串 (Predefined format strings)

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是我们正在使用的预定义格式字符串的名称。

在Express中使用Morgan (Using morgan with Express)

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 */));

格式化字符串 (Format Strings)

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)

Tokens are the keywords following the :’s. Tokens are defined as functions. We’ll look at making custom tokens next.

标记是:后面的关键字。 令牌被定义为功能。 接下来,我们将介绍制作自定义令牌。

创建我们自己的令牌 (Creating Our Own Tokens)

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将向函数传递两个参数: reqres 。 让我们创建一个令牌,该令牌显示发送请求的域。

morgan.token('host', function(req, res) {    return req.hostname;});

使用我们的自定义令牌 (Using our custom tokens)

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.

使用自定义参数创建自定义标记 (Creating Custom Tokens with Custom Arguments)

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');

将morgan与内置http模块一起使用 (Using morgan with the Built-In http Module)

Using morgan with the Node.js built-in http module is easy as well. Here’s an .

使用Node.js内置http模块的morgan也很容易。 这是的 。

We’re done, yay! That’s all we’re going to cover in this article, but I recommend you checkout the . Stay tuned, we might have more morgan articles coming soon too! Thanks for reading!

我们完成了,是的! 这就是我们将在本文中介绍的全部内容,但是我建议您查阅 。 请继续关注,我们可能还会很快发布更多摩根文章! 谢谢阅读!

翻译自:

morgan logger

转载地址:http://rihgb.baihongyu.com/

你可能感兴趣的文章
hibernate中持久化对象的生命周期(三态:自由态,持久态,游离态 之间的转换)...
查看>>
postgres出现Server doesn't listen错误解决办法
查看>>
linux shell学习--awk练习
查看>>
敏捷开发一千零一问系列之十二:敏捷实施的步骤?
查看>>
TCP三次握手机制中的seq和ack
查看>>
java内部类的定义原则
查看>>
2017年11月26日 C#流&&窗体对话框
查看>>
endl与\n的区别
查看>>
进程和线程概念及原理
查看>>
Dubbo超时重试机制带来的数据重复问题
查看>>
注解配置里的几个注解
查看>>
使ie678支持css3伪类选择器的插件
查看>>
题解报告:hdu 1212 Big Number(大数取模+同余定理)
查看>>
POJ 3624 Charm Bracelet
查看>>
ZOJ 2314 Reactor Cooling
查看>>
关于做事的一点想法
查看>>
程序在本地能启动而预发布不能启动
查看>>
Lucene、ES好文章
查看>>
有关定时器setTimeout()、setInterval()详解
查看>>
刷题总结——次小生成树(bzoj1977 最小生成树+倍增)
查看>>