Use the node.js “debug” module to avoid log pollution applications

This article shared everyone about the use of the Node.js “Debug” module to avoid log pollution application logs. Xiaobian feels quite practical, so share it with you to do a reference. Let’s take a look at Xiaobian.

Have you had such an experience? When you add a logging to a custom Node module, and think you will benefit from these additional information, but you find that when you add the module as a dependency And run npm install and npm start , your program log will be like below?

How do you find the information you need? For example, what are the users log in and when logging in, not from the application dependencies: 使用Node.js “debug”模块避免日志污染应用程序日志的方法

, when you suspect that your team is dependent on the Node module When the changes have been changed, the only logs that need to be resolved are as follows: 使用Node.js “debug”模块避免日志污染应用程序日志的方法

From the global perspective, this is a log pollution:

使用Node.js “debug”模块避免日志污染应用程序日志的方法

We want

The ideal logging scheme enables the module log to enter the program or service log record, but open it to troubleshoot it when needed.

To this end, I will show you how to use the Debug module to open or close the log in the Node module. 使用Node.js “debug”模块避免日志污染应用程序日志的方法

If you don’t need them, and use the service of the module to run well, set it to




Used to encounter problems and require as many log information as possible.

DEBUG Module Overview

In terms of function, don’t consider debug modules like usually in the debugger (just like you set breakpoints in ID E) The same is true), but it should be more like being modified

Console module. This is a kind ofIt lets you turn on and off as needed logging tools. If its GitHub page (https: // …), it will also see its various other features such as namespaces and so on. Don’t use these temporary, now just know that it is a tool that optimizes your production environment.



let us see an example of using

Debug in the code. Suppose is a module below:

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’);});

Implementation Debug It is easy. What we have to do is import or require module, then create a logger –

const log = debug (‘http: server’); . You only need to pass a specific logger’s tag or namespace ‘http: server’ to
 Debug  environment variable to enable debugging.  
When the application is started (ie the program that imports the module), you will start it like this:

Debug = http: server node app-entrypoint.js What if we want to use a separate logger to switch some logs? For example, if we just want to switch to Error? Just as simple as the other logger, just like this: const debug = Require (‘debug’); const log = debug (‘http: server’); const errorlogger = Debug (‘http: error’); // Create New Logger Here, Passing It A New NamespaceConst http = Require (‘http’); const name = ‘demo app for debug module’; log (‘booting% o’, Name ); http.createserver ((REQ, RES) => {log (Req.Method + ” + Req.ur); res., ‘Hello \ n’); // fake an error to demonstrate useing debug for http : Error Namespace setTimeout (() => {ErrorLogger (‘favor error’); // Ten Call the logger}, 1000);}). Listen (3200, () => {log (‘listening’);})

The following will be opened only:
   Debug= http: error node app-entrypoint.js 
And, when you want to record the log of all namespaces (in this case, it is

http: server

And http: error
), just transfer wildcard  * . 
debug = http: * node app-entrypoint.js

Let’s take a look at what these logs are:
   Then, when we load the page: 

Note, Debug will be written STDERR instead of stdout , so if you have a log route that extracts the log according to different sources, these debug logs will eventually be stored in the Stderr stream.

Finally, you can also choose:
   You can record the log of all modules written in 
Debug .

or, you can still record some content with

console.log ()

/ 使用Node.js “debug”模块避免日志污染应用程序日志的方法, while others record


使用Node.js “debug”模块避免日志污染应用程序日志的方法 If you have something that must be recorded, and use


to record it with console . But if you don't do this, you will use Debug to record all content. Open Debug

to now, if you (or your team) written the module, then the content we introduce is great. .

  • But if you use a third-party module, what should I do when you need to open debugging? Maybe you have troubleshooting the various parts of the program, not enough now, suppose you are using the open source module that may make you encounter.
  • Good news is,
  • Debug is a very broad module, many modules in npm may be in use. The creator of the third party module knows (at least, should know ) The user of the module really does not want to record by default because all other logs may become messy. Since the DEBUG module is used inside the Express, if you use Express and you need to troubleshoot it, you can open the log in the following command:

Debug = Express: * node your-app-entrypoint.js Whether the open source Node module is used Debug

and how to use

What should I do if I don’t see if I use


in the document?

Even so, it is also possible to use the module, but you have to make some mining.

We already know how to use it in the Express document, but we assume that it is not recorded:

First, you can be on the project GitHub (or other open source website Search


, then find modules listed as dependencies:

After confirming, you also need to confirm 1) actual In use, and 2) which flags need to be used. GitHub now has a good feature that you can use the search bar to search the item itself. If we search for “debug” in the Express project, you will find the following:

We can see that the switch is marked as “express”, so
 express: *  will provide us with  all 
log, Express : View

will provide us with a view of the view, and so on. Thank you for reading! With regard to the use of node.js “debug” module to avoid log pollution application logs, share it here, I hope that the above can help everyone, let everyone learn more. If you feel that the article is good, you can share it out to let more people see it!

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment