iflog
v0.3.0
Published
A conditional console.log that can be toggled on demand
Maintainers
Readme
iflog
A conditional console utility that only logs in development mode. It wraps all standard console methods and only outputs when process.env.NODE_ENV !== 'production'. Now with URL parameter override and manual controls for production debugging!
Installation
npm i iflogUsage
import iflog from "iflog";
iflog.log("This will only log in development!");
iflog.error("This error will only show in development!");
iflog.table([{ a: 1 }, { a: 2 }]);
if (iflog.isEnabled()) {
// do something only when logging is enabled
}
// Manual control
iflog.enable(); // Force enable logging (even in production)
iflog.disable(); // Force disable logging (even in development)Production Debugging
Need to debug logs in production? You have two options:
1. URL Parameter Override
Simply add ?iflog=true to your URL:
https://yoursite.com/page?iflog=true2. Manual Enable/Disable
Use the enable() and disable() methods for programmatic control:
iflog.enable(); // Force logging on
iflog.log("This will show even in production");
iflog.disable(); // Force logging offBoth methods will override the environment detection and give you full control over iflog's behavior. This enables you to add keyboard shortcuts to toggle logging in production or bind to a button in your app.
How it works
All methods are no-ops in production (process.env.NODE_ENV === 'production'), except when:
- The URL parameter
?iflog=trueis present, OR iflog.enable()has been called
In development, they proxy to the corresponding console methods, unless iflog.disable() has been called.
API
All methods below only output when iflog is enabled (development mode, URL override, or manually enabled):
iflog.assert(condition, ...args)iflog.clear()iflog.count(label)iflog.countReset(label)iflog.debug(...args)iflog.dir(...args)iflog.dirxml(...args)iflog.error(...args)iflog.exception(...args)(alias for error)iflog.group(...args)iflog.groupCollapsed(...args)iflog.groupEnd()iflog.info(...args)iflog.log(...args)iflog.profile(label)iflog.profileEnd(label)iflog.table(tabularData, properties)iflog.time(label)iflog.timeEnd(label)iflog.timeLog(label, ...args)iflog.timeStamp(label)iflog.trace(...args)iflog.warn(...args)
Control Methods
iflog.enable()- Force enable logging regardless of environmentiflog.disable()- Force disable logging regardless of environmentiflog.toggle()- Toggle between enabled and disabled statesiflog.isEnabled()- Returnstrueif logging is enabled,falseotherwiseiflog.reset()- Reset to default behavior
License
MIT
