scoped-ts-log
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

scoped-ts-log

Scoped TypeScript Log provides a typed logging utility for JavaScript/TypeScript projects. All events logged by the utility require a named scope, which can be used to filter and remove events.

Usage

Install via NPM

npm i -S scoped-ts-log

Import in JavaScript

import { Log } from 'scoped-ts-log'
Log.add("DEBUG", "This is a logged debug event.", "scope")
// Or using a shorthand:
Log.debug("This is a logged debug event.", "scope")

Features

The package provides a global scope object Log that can be used to store, examine and export messages and events.

Events must be classified at one of the accepted priority levels (DEBUG, INFO, WARN, and ERROR). A special level DISABLE is not meant for event classification; it can be used as a threshold to prevent any log messages from being printed to the JavaScript console.

Suggested classification scheme:

  • DEBUG: Lowest level events meant to track the normal operation of the application.
  • INFO: Used to notify that an important operation has completed, for example a module or resource finishes loading.
  • WARN: Used when an expected issue prevents the application from fully performing, but not altogether stopping, an operation.
  • ERROR: Used when an (usually) unexpected issue prevents the application from continuing an operation.

Each of these levels have their own shorthand, namely Log.debug(), Log.info(), Log.warn(), and Log.error(). The three former accept as arguments a message and scope (both strings). The error shorthand also accepts a third argument for the actual Error that was thrown.

The base Log.add() method accepts an optional fourth argument extra that can hold anything related to the event.

Handling events from web workers

Since the worker opertes in a different scope than the main document, the Log objects imported in workers are separate objects. Since we don't want to maintain multiple instances of the object, all with their individual event buffers, we can register a worker to automatically relay all events from its Log to the Log where it's registered (i.e. the main document).

import { Log } from 'scoped-ts-log'
const worker = new Worker()
Log.registerWorker(worker)
// Now all Log messages from worker are relayed to local Log.

Readme

Keywords

Package Sidebar

Install

npm i scoped-ts-log

Weekly Downloads

8

Version

2.0.0

License

MIT

Unpacked Size

38.7 kB

Total Files

4

Last publish

Collaborators

  • sam-19