This article will introduce you to how to use the Node.js "debug" module to avoid log pollution of application logs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Video tutorial recommendation: nodejs tutorial
Have you ever had such an experience, please record it into a custom Node module, thinking you would benefit from the extra information, only to find that when you add the module as a dependency and run npm install
and npm start
, will your program log look like the following?
#How do you find the information you actually need? Like who logged in and when, rather than information from application dependencies:
Likewise, when you suspect that your When a change the team made to a Node module dependency caused performance degradation, the only logs that needed to be addressed were:
Looking at the big picture, this is Here’s what log pollution looks like:
The ideal logging solution is one where module logs do not enter the logging of a program or service, but Ability to open it for troubleshooting when needed.
To do this, I will show you how to use the debug module to turn logging on or off in the Node module.
If you don't need them and the service using this module runs fine, set it to OFF.
ON Used when you encounter a problem and need as much log information as possible.
debug
Module Overview In terms of functionality, please do not think of debug modules as you would normally think of them in a debugger (like how you set breakpoints in the IDE) in the same way), but should be more like a modified console
module. This is a tool that allows you to turn logs on and off as needed. If you go to its GitHub page (https://github.com/visionmedi...), you will also see its various other features, such as namespaces, etc. Ignore that for now, just know that it is a tool that can optimize your production environment.
debug in your own Node module
Let’s look at an example of using debug
in your code. Assume the following is a module:
const debug = require('debug'); const log = debug('http:server'); const http = require('http'); const name = 'Demo App for debug module'; log('booting %o', name); http.createServer((req, res) => { log(req.method + ' ' + req.url); res.end('debug example\n'); }).listen(3200, () => { log('listening'); });
Implementing debug
is easy. All we have to do is import or require the module, and then create a logger - const log = debug('http:server');
. You just need to pass the tag or namespace 'http:server' of a specific logger to the DEBUG
environment variable to enable debugging.
When you start the application (that is, the program that imported the module), you will start it like this:
DEBUG=http:server node app-entrypoint.js
If we want What should I do if I use a separate logger to switch certain logs? For example, what if we only want to switch to error? It's as simple as creating another logger, like this:
const debug = require('debug'); const log = debug('http:server'); const errorLogger = debug('http:error'); // create new logger here, passing it a new namespace const http = require('http'); const name = 'Demo App for debug module'; log('booting %o', name); http.createServer((req, res) => { log(req.method + ' ' + req.url); res.end('hello\n'); // fake an error to demonstrate using debug for http:error namespace setTimeout(() => { errorLogger('fake error'); // then call the logger }, 1000); }).listen(3200, () => { log('listening'); });
The following will only open errors:
#
DEBUG=http:error node app-entrypoint.js
而且,当你要记录所有命名空间的日志时(在这种情况下,是 http:server
和 http:error
),只需传递通配符 *
即可。
DEBUG=http:* node app-entrypoint.js
让我们看看这些日志现在是什么样的:
然后,当我们加载页面时:
注意,debug
会写入 stderr
而不是 stdout
,所以如果你有一个根据不同的来源提取日志的日志路由,这些调试日志将最终会存储在 stderr 流中。
最后,你还可以选择:
debug
编写的模块的日志。console.log()
/console.error()
记录某些内容,而另一些记录 debug
如果你有必须要记录的事项,并且与使用该模块的应用程序有关,请用 console
将其记录下来。但是如果你不这样做,就会用 debug
记录所有的内容。
debug
到目前为止,如果你(或你的团队)编写了该模块,那么我们所介绍的内容就很棒。
但是如果你用的是第三方模块,那需要打开调试时该怎么办?
也许你已经对程序的各个部分进行了故障排除,不够现在假设你正在使用的开源模块可能会让你遇到问题。
好消息是,debug
是一个使用非常广泛的模块,npm 中的许多模块可能都在用它。第三方模块的创建者知道(至少,应该知道)该模块的用户真的不希望默认情况下进行记录,因为所有其他的日志都可能会变得杂乱无章。
由于 Express 内部使用了 debug 模块,所以如果你用了 Express 并需要对其进行故障排除,则可以通过运行以下命令打开日志:
DEBUG=express:* node your-app-entrypoint.js
debug
以及是如何使用的如果在文档中没有看到关于是否使用了 debug
的内容该怎么办? 即便如此,也有可能使用了该模块,但是你必须进行一些挖掘。
我们已经知道 Express 文档中描述了该如何使用它,但是我们假设它并没有记录在案:
首先,你可以在项目的 GitHub(或其他开放源代码网站)上搜索 package.json
,然后查找其中列出的作为依赖项的模块:
确认后,你还需要确认 1)实际正在使用中,以及 2)切换需要使用哪些标志。 GitHub 现在具有一项不错的功能,你可以在其中使用搜索栏搜索项目本身。如果我们在 Express 项目中搜索“debug”,则会发现以下内容:
我们可以看到切换标记为 “express”,因此 express:*
将为我们提供所有的日志,express:view
将为我们提供视图的日志,等等。
希望你看到在 node 模块中实现 debug
并为第三方模块打开它是多么容易,以及它可以怎样帮你清理日志,同时又为你提供了一种方便的方法来将其重新打开进行故障排除。
英文原文地址:https://www.coreycleary.me/using-the-debug-module-to-avoid-polluting-your-application-logs-with-logs-from-node-modules/
作者:Corey Cleary
更多编程相关知识,可访问:编程入门!!
The above is the detailed content of Avoid log pollution of application logs using the Node.js 'debug” module. For more information, please follow other related articles on the PHP Chinese website!