the-browser-logger

A logger for node or browser

Usage no npm install needed!

<script type="module">
  import theBrowserLogger from 'https://cdn.skypack.dev/the-browser-logger';
</script>

README

The browser logger

Why

Needed a simple logging module that can be enabled or disabled per module

How to use it?

First you need to import it in your project

The require way

let { requestLogger } = require("the-browser-logger");

The import way

import { requestLogger } from "the-browser-logger";

Declare the module, class or service where you want to instanciate the browser logger

class myClass {
  constructor(id) {
    this._id = id
    setTimestampFormat(true)
    setLoggerLevel(Level.WARNING)
  }

  _logger() {
    return requestLogger(this._id);
  }
}

Then call it to report different types of logs

setTimestampFormat(true)
setLoggerLevel(Level.WARNING)

_myfunction() {
  try {
    _logger().debug("_myfunction()", "debug logging");
    _logger().log("_myfunction()", "log logging");
    _logger().info("_myfunction()", "info logging");
    _logger().warn("_myfunction()", "warn logging");
  } catch (e) {
    _logger().error("_myfunction()", "error logging", e);
  }
}

Logger level is set to NONE by default, these are the available levels:

  • NONE (logging disabled)
  • ERROR (error only)
  • WARNING (warning logs including errors)
  • INFO (info logs including warning and error)
  • LOG (include all above and the one called with .log)
  • DEBUG (most verbose)
import { setTimestampFormat, setLoggerLevel, Level, requestLogger } from "the-browser-logger";
setTimestampFormat(true)
setLoggerLevel(Level.WARNING)

_logger() {
  return requestLogger("myClass");
}

myfunction() {
  _logger().debug("_myfunction()", "debug logging");  // Not shown log due level
  _logger().log("_myfunction()", "log logging");  // Not shown due level
  _logger().info("_myfunction()", "info logging");  // Not shown log due level
  _logger().warn("_myfunction()", "warn logging");
  _logger().error("_myfunction()", "error logging");
}

myfunction()

1606687648056 WARN main ["_myfunction()", "warn logging"]
1606687648056 ERROR main ["_myfunction()", "error logging"]

You can always refer to library documentation here

Powered by Deepertech