@whi/weblogger
v0.4.0
Published
A minimal logger implementation designed to work in web browsers
Downloads
104
Readme
new Logger( context, level )
This micro-package provides a minimalist logging class for web browsers.
Overview
Bundled size is less than 2KB
Install
npm i @whi/weblogger
Usage
Browser
<script src="weblogger.bundled.js"></script>
<script type="text/javascript">
const { Logger } = WebLogger;
const log = new Logger( "main" );
log.fatal("Testing");
log.error("Testing");
log.warn("Testing");
log.normal("Testing"); // default level
log.info("Testing");
log.debug("Testing");
log.trace("Testing");
</script>
Node
const { Logger } = require('@whi/weblogger');
const log = new Logger( "main", "debug" );
log.fatal("Testing");
log.error("Testing");
log.warn("Testing");
log.normal("Testing");
log.info("Testing");
log.debug("Testing");
log.trace("Testing"); // would not log
Set defaults using localStorage
window.localStorage.setItem("LOG_COLOR", "false"); // turn off coloring
window.localStorage.setItem("LOG_LEVEL", "debug"); // set default level to "debug"
Avoiding expensive arg computations
Short-circuit using logical operators
log.level.debug && log.debug("Expensive arg computation: %s", value.map(...).join(", ") );
Or, a callback function that only evaluates when a message will be logged.
log.debug("Expensive arg computation: %s", () => [ value.map(...).join(", ") ]);
API Reference
new Logger( context, level, colors )
Change this Logger's verbosity level.
context
- (required) an identifier used in the log formatlevel
- (optional) the starting log level- defaults to level 3 (normal)
colors
- (optional) a boolean for log coloring- defaults to
true
- defaults to
<Logger>.setLevel( level )
Change this Logger's verbosity level.
level
- (required) the new log level
Returns the integer value of the new level.
Contributing
See CONTRIBUTING.md