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-loggerimport{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{instanceasnl,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';constnl=newNullishLogger();nl.suppress=['error','warn'];constdebug=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{instanceasnl,debug}from'nullish-logger';constdebug=newNullishLogger().logger;if(process.env.NODE_ENV==='production'){nl.enabled=false;}debug?.log('Initializing...');// only outputs in dev envWhen 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.
<scripttype="module">import{debug}from'https://unpkg.com/nullish-logger/nullish-logger.min.js';debug?.log('Browser logging works!');</script>