Skip to content

how to avoid the tedious initialization? #154

Closed

Description

@stablum

also posted here: http://stackoverflow.com/questions/26777433/node-js-debug-module-how-to-avoid-this-tedious-initialization

I am wondering if there is any way to prevent this tedious and overly redundant inizialization in each module:

require('debug').enable('module-name:log module-name:ERROR');
var log = require('debug')('module-name:log');
var error = require('debug')('module-name:ERROR');

As you can see it's redundant on two different levels: I need to "enable" the loggers before instantiating them, sending the same name twice as strings, and I need to write the module name. Is there any way to do this automagically?

Activity

  1. changed the title [-]node.js' debug module: how to avoid this tedious initialization?[/-] [+]how to avoid the tedious initialization?[/+] on Nov 7, 2014
  2. ibc commented on Nov 16, 2014

    @ibc
    Contributor

    That is how the module is designed. Anyhow take a look to #156

  3. nikcorg commented on Nov 18, 2014

    @nikcorg

    I could be wrong, but you may be missing the point slightly; debug shouldn't merely be treated as a drop-in-replacement for console.log with pretty colours, albeit it can be, if you enable all output, i.e. export DEBUG="*" for node.js environments and debug.enable("*") for browsers.

    The power of debug is really in being able to see only targeted output, instead of trying to find the correct lines in a torrent of text whizzing by. That's where namespacing and module names come to your aid.

    However, to answer you question:

    I tend to use browserify for bundling, enabling/disabling output in my entry point file. Combined with envify it becomes a breeze to enable or disable debug output in the same manner for all environments. (I.e. using environment variables during runtime/build time.)

  4. dmarcelino commented on Mar 11, 2015

    @dmarcelino
    Contributor

    Hi @stablum, I've just replied to your stackoverflow question. If you are using debug outside the browser you can use debug-logger to lazily instantiate multiple instances of debug to log at different levels as you would do with console. It's essentially a wrapper for debug with no other dependencies.

    Example:

    var log = require('debug-logger')('myapp');
    
    log.trace("I'm a trace output");
    log.debug("I'm a debug output");
    log.log("I'm a log output");
    log.info("I'm an info output");
    log.warn("I'm a warn output");
    log.error("I'm an error output");

    would print:

    screenshot

    More info at the project page,
    Regards

  5. ashwell commented on Oct 6, 2016

    @ashwell

    I know this is a year later, but I just wrote this to help facilitate creating debug instances for stdout and stderr...

    const debug = require( 'debug' );
    
    module.exports = function( namespace ) {
      const
        log = debug( `${ namespace }:log` ),
        error = debug( `${ namespace }:error` );
    
      log.log = console.log.bind( console );
    
      return { log, error };
    };

    threw that in a utils folder and require/use it just like you would debug, just with a little added destructuring magic

    const {
      log,
      error
    } = require( '../utils/debug-helper' )( 'module-name' );
  6. thebigredgeek commented on Dec 14, 2016

    @thebigredgeek
    Contributor

    Closing this for now. Feel free to discuss further the V3 API RFC issue: #370

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions