1

In a Node API I'm building I want a quick and dirty way of creating a global bool to enable (or disable) some debug logging. I'm doing this right now:

In main.js (main entry point) I've got:

global.DEBUG = true;

And then in any modules I use, I can do:

if (DEBUG) { 
    // then do stuff 
}

Is there anything wrong with doing it this way? Is there a more appropriate way and if so, why is it a better way?

3 Answers 3

4

You're better off doing this with environment variables.

var DEBUG = process.env.DEBUG;
if (DEBUG) {
  // then do stuff
}

Then start your app

$ DEBUG=TRUE node app.js
Sign up to request clarification or add additional context in comments.

2 Comments

I was going to say the same thing. I usually use an environment variable called LOG_LEVEL that can be ERROR, WARN, INFO, DEBUG, etc.
I like it. Got me thinking I should go look for this too haha
0

You can do this :

global.debug = true ;// or false

//and in any file

if(global.debug){
   // something
}

Comments

-1

Use a config-module and include it if needed:

//conf.js
module.exports = {
DEBUG: true
}

//other.js
if(require('./conf').DEBUG)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.