A lightweight, configurable javascript console wrapper with selective method suppression. Perfect for toggling debug output without removing logging code.
- Toggle all logging output at will
- Selectively suppress specific log methods
- Runtime configuration
- No dependencies
- Preserves original console behavior
- Zero global scope pollution
- Terse syntax
npm install nullish-logger
import { debug } from 'nullish-logger';
debug?.log('This works like console.log');
debug?.table({ 'tested' : true });
debug?.info('all console features are available');
Essentially, you just use it as a direct replacement to the console
object. With one key difference. Use the optional chaining operator
to access the methods.
import { instance as nl, debug } from 'nullish-logger';
nl.enabled = false;
debug?.log('suppressed');
nl.enabled = true;
debug?.log('now it works');
debug?.warn('suppressed');
nl.quiet = false;
debug?.warn('not suppressed anymore');
import { NullishLogger } from 'nullish-logger';
const nl = new NullishLogger();
nl.suppress = [ 'error', 'warn' ];
const debug = nl.logger;
debug?.error('suppressed');
debug?.warn('also suppressed');
debug?.info('works');
enabled
(boolean) - Master switch for all loggingquiet
(boolean) - When true, suppresses methods in suppress listsuppress
(string[]) - Array of console methods to suppress when quiet
Automatically enable/disable verbose logging based on the detected environment.
import { instance as nl, debug } from 'nullish-logger';
const debug = new NullishLogger().logger;
if (process.env.NODE_ENV === 'production') {
nl.enabled = false;
}
debug?.log('Initializing...'); // only outputs in dev env
When NullishLogger
is disabled. debug === null
. So you can use it as a conditional for running other debug code.
debug && doSomething();
if (debug && !test()) {
debug?.warn('uh-oh');
} else {
debug?.info('perfect');
}
NullishLogger works just as well in browser as it does in Node.
<script type="module">
import { debug } from 'https://unpkg.com/nullish-logger/nullish-logger.min.js';
debug?.log('Browser logging works!');
</script>