1

I basically want to be able to "turn on debug logging" like you can do with many applications that run in a shell. The naive way of implementing it would be to insert a statement like this in every one of my functions:

if (DEBUG) console.log('Executing <name of function>');

But having this in every function seems a bit annoying. Is there a more established or cleaner way of doing this?

asked Sep 17, 2020 at 4:41

2 Answers 2

2

You can specify the level in the logging statement, like this

console.debug('this is a debug level log statement');
console.info('this is an info level log statement');
console.warn('this is a warn level log statement');

In your console viewer (for example Developer Tools in Chrome) you can select which log messages you want to view.

See also https://developer.mozilla.org/en-US/docs/Web/API/console

answered Sep 17, 2020 at 7:30
1

The way I am dealing with it rigth now is by having my logger a singleton/dependency injected object so that I just have to write

logger.debug('Something happening')

which obviously contains

function debug(msg){ if (this.DEBUG) console.log(msg) }

Then I just have to initialise it with the proper log level that I want when loading it.

answered Sep 17, 2020 at 7:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.