Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to avoid the tedious initialization? #154

Closed
stablum opened this issue Nov 7, 2014 · 5 comments
Closed

how to avoid the tedious initialization? #154

stablum opened this issue Nov 7, 2014 · 5 comments

Comments

@stablum
Copy link

stablum commented Nov 7, 2014

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?

@stablum stablum changed the title node.js' debug module: how to avoid this tedious initialization? how to avoid the tedious initialization? Nov 7, 2014
@ibc
Copy link
Contributor

ibc commented Nov 16, 2014

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

@nikcorg
Copy link

nikcorg commented Nov 18, 2014

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.)

@dmarcelino
Copy link
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

@ashwell
Copy link

ashwell commented Oct 6, 2016

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' );

@thebigredgeek
Copy link
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
Labels
None yet
Development

No branches or pull requests

6 participants