Skip to content
Related Articles

Related Articles

Node.js util.debuglog() Method
  • Last Updated : 31 Aug, 2020

The “util” module provides ‘utility’ functions that are used for debugging purposes. For accessing those functions we need to call them (by ‘require(‘util’)‘).

The util.debuglog() (Added in v0.11.3) method is an inbuilt application programming interface of the util module which is used to create a function that is based on the NODE_DEBUG environment variable which writes debug messages conditionally to stderr. The returned function operates in the same way as console.error() if the section name appears inside the value of that environment variable. If not, then the returned function is a no-op.

Syntax:

util.debuglog(section[, callback])

Parameters: This function accepts two parameters as mentioned above and described below:

  • section <string>: It accepts an <string> identifying the portion of the application for which the debuglog function is being created.
  • callback <Function>: It accepts the callback invoked the first time the logging function is called with a function argument that is a more optimized logging function..

Return Value <Function>: It Returns the logging function.



Example 1: Filename: index.js

Javascript




// Node.js program to demonstrate the 
// util.debuglog() method 
  
// Using require to access util module 
const util = require('util');
  
const debugLog = util.debuglog('run-app');
  
// Use debuglog() method
debugLog('hello from my debugger [%d]', 123);
// SET NODE_DEBUG=run-app&&node util.js
  
// Another way to import debuglog
const { debuglog } = require('util');
  
const debuglogue = debuglog('run-app1');
  
// Use debuglog() method
debuglogue('hello from run-app [%d]', 123);
  
const a = "old Value";
  
let deebuglog = util.debuglog('run-app2',
    (debuging) => {
  
        // Replace with a logging function
        // that optimizes out
        a = "new Value";
  
        // Testing if the section is enabled
        deebuglog = debuging;
    });
  
// prints the debuglog function 
console.log(util.inspect(deebuglog, 
    showHidden = true, compact = true));
  
// Prints nothing
console.log(a);
  
// logs app *
deebuglog();
  
deebuglog('hi there, it\'s run-app [%d]', 2333);

Run index.js file using the following command:

SET NODE_DEBUG=run-app*&&node index.js
OR 
NODE_DEBUG=run-app* node index.js

Output:

>> RUN-APP 8112: hello from my debugger [123]

>> RUN-APP1 8112: hello from run-app [123]

>> <ref *1> [Function (anonymous)] {…… [prototype]: { [constructor]: [Circular *1] }

}



>> old Value

>> RUN-APP2 8112:

>> RUN-APP2 8112: hi there, it’s run-app [2333]

Example 2: Filename: index.js

Javascript




// Node.js program to demonstrate the 
// util.debuglog() method 
  
// Using require to access util module 
const util = require('util');
// const {debuglog} = require('util');
  
const debuglog = util.debuglog('alfa-beta');
  
debuglog('Hii there, debuglog from alfa-beta [%d]', 2333);
  
const generalLog = util.debuglog('alfa-');
const timerLog = util.debuglog('alfa-romeo');
const delay = 800;
  
generalLog('Leaving alfa-...');
console.log("Wait for timerLog...")
setTimeout(() => {
    timerLog('timer fired after %d ', delay);
}, delay);

Run index.js file using the following command:

SET NODE_DEBUG=alfa-*&&node index.js
OR
NODE_DEBUG=alfa-* node index.js

Output:

ALFA-BETA 7008: Hii there, debuglog from alfa-beta [2333]

ALFA- 7008: Leaving alfa-…

Wait for timerLog…

ALFA-ROMEO 7008: timer fired after 800

Reference: https://nodejs.org/api/util.html#util_util_debuglog_section_callback

My Personal Notes arrow_drop_up
Recommended Articles
Page :