This micro-package provides a minimalist logging class for web browsers.
Bundled size is less than 2KB
npm i @whi/weblogger
<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>
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
window.localStorage.setItem("LOG_COLOR", "false"); // turn off coloring
window.localStorage.setItem("LOG_LEVEL", "debug"); // set default level to "debug"
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(", ") ]);
Change this Logger's verbosity level.
-
context
- (required) an identifier used in the log format -
level
- (optional) the starting log level- defaults to level 3 (normal)
-
colors
- (optional) a boolean for log coloring- defaults to
true
- defaults to
Change this Logger's verbosity level.
-
level
- (required) the new log level
Returns the integer value of the new level.
See CONTRIBUTING.md