Function debug

  • The util.debuglog() method is used to create a function that conditionally writes debug messages to stderr based on the existence of the NODE_DEBUGenvironment variable. If the section name appears within the value of that environment variable, then the returned function operates similar to console.error(). If not, then the returned function is a no-op.

    const util = require('util');
    const debuglog = util.debuglog('foo');

    debuglog('hello from foo [%d]', 123);

    If this program is run with NODE_DEBUG=foo in the environment, then it will output something like:

    FOO 3245: hello from foo [123]
    

    where 3245 is the process id. If it is not run with that environment variable set, then it will not print anything.

    The section supports wildcard also:

    const util = require('util');
    const debuglog = util.debuglog('foo-bar');

    debuglog('hi there, it\'s foo-bar [%d]', 2333);

    if it is run with NODE_DEBUG=foo* in the environment, then it will output something like:

    FOO-BAR 3257: hi there, it's foo-bar [2333]
    

    Multiple comma-separated section names may be specified in the NODE_DEBUGenvironment variable: NODE_DEBUG=fs,net,tls.

    The optional callback argument can be used to replace the logging function with a different function that doesn't have any initialization or unnecessary wrapping.

    const util = require('util');
    let debuglog = util.debuglog('internals', (debug) => {
    // Replace with a logging function that optimizes out
    // testing if the section is enabled
    debuglog = debug;
    });

    Returns

    The logging function

    Parameters

    • section: string

      A string identifying the portion of the application for which the debuglog function is being created.

    • Optional callback: ((fn: "util".DebugLoggerFunction) => void)

      A callback invoked the first time the logging function is called with a function argument that is a more optimized logging function.

    Returns "util".DebugLogger

  • The util.debuglog() method is used to create a function that conditionally writes debug messages to stderr based on the existence of the NODE_DEBUGenvironment variable. If the section name appears within the value of that environment variable, then the returned function operates similar to console.error(). If not, then the returned function is a no-op.

    const util = require('util');
    const debuglog = util.debuglog('foo');

    debuglog('hello from foo [%d]', 123);

    If this program is run with NODE_DEBUG=foo in the environment, then it will output something like:

    FOO 3245: hello from foo [123]
    

    where 3245 is the process id. If it is not run with that environment variable set, then it will not print anything.

    The section supports wildcard also:

    const util = require('util');
    const debuglog = util.debuglog('foo-bar');

    debuglog('hi there, it\'s foo-bar [%d]', 2333);

    if it is run with NODE_DEBUG=foo* in the environment, then it will output something like:

    FOO-BAR 3257: hi there, it's foo-bar [2333]
    

    Multiple comma-separated section names may be specified in the NODE_DEBUGenvironment variable: NODE_DEBUG=fs,net,tls.

    The optional callback argument can be used to replace the logging function with a different function that doesn't have any initialization or unnecessary wrapping.

    const util = require('util');
    let debuglog = util.debuglog('internals', (debug) => {
    // Replace with a logging function that optimizes out
    // testing if the section is enabled
    debuglog = debug;
    });

    Returns

    The logging function

    Parameters

    • section: string

      A string identifying the portion of the application for which the debuglog function is being created.

    • Optional callback: ((fn: "util".DebugLoggerFunction) => void)

      A callback invoked the first time the logging function is called with a function argument that is a more optimized logging function.

    Returns "util".DebugLogger

Generated using TypeDoc