Open In App

Node.js util.debuglog() Method

Last Updated : 01 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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',
    (debugging) => {
 
        // Replace with a logging function
        // that optimizes out
        a = "new Value";
 
        // Testing if the section is enabled
        deebuglog = debugging;
    });
 
// 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



Previous Article
Next Article

Similar Reads

Node.js util.types.isMap() Method
The util.types.isMap() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isMap() method is used to determine whether the value is a built-in Map instance or not. Syntax: util.types.isMap( value ) Parameters: This method accepts a single
2 min read
Node.js util.types.isMapIterator() Method
The util.types.isMapIterator() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js internal APIs. The util.types.isMapIterator() method is used to determine whether the value is an iterator returned for a built-in Map instance or not. Syntax: util.types.isMapIterator( val
2 min read
Node.js util.types.isSetIterator() Method
The util.types.isSetIterator() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isSetIterator() method is used to determine whether the value is an iterator returned for a built-in set instance. Syntax: util.types.isSetIterator( value
2 min read
Node.js util.types.isWeakMap() Method
The util.types.isWeakMap() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isWeakMap() method is used to determine whether the value is a built-in WeakMap instance or not. Syntax: util.types.isWeakMap( value ) Parameters: This method
2 min read
Node.js util.types.isWeakSet() Method
The util.types.isWeakSet() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isWeakSet() method is used to determine whether the value is a built-in WeakSet instance or not. Syntax: util.types.isWeakSet( value ) Parameters: This method
2 min read
Node.js util.types.isBooleanObject() Method
The util.types.isBooleanObject() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isBooleanObject() method is used to determine whether the value is a Boolean object or not. Syntax: util.types.isBooleanObject( value ) Parameters: This
2 min read
Node.js util.types.isPromise() Method
The util.types.isPromise() method is an inbuilt application programming interface of the util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isPromise() method is used to determine whether the value is a built-in Promise. Syntax: util.types.isPromise( value ) Parameter: This method accepts a sin
2 min read
Node.js util.types.isTypedArray() Method
The util.types.isTypedArray() method is an inbuilt application programming interface of util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isTypedArray() method is used to determine whether the value is a built-in TypedArray instance. Syntax: util.types.isTypedArray( value ) Parameter: This met
1 min read
Node.js util.types.isSymbolObject() Method
The util.types.isSymbolObject() method is an inbuilt application programming interface of the util module which is primarily designed to support the needs of Node.js own internal APIs. The util.types.isSymbolObject() method is used to determine whether the value is a symbol object. Syntax: util.types.isSymbolObject( value ) Parameter: This method a
2 min read
Node.js util.types.isNumberObject() Method
The util.types.isNumberObject() method of util module is primarily designed to support the needs of Node.js own Internal APIs. It is used to check whether the passed value is a Number object or not. Syntax util.types.isNumberObject( value ) Parameters: This method accepts a single parameter value which holds any number i.e. instance of any module.
2 min read