npm.io
1.0.5 • Published 4 years ago

@tnotifier/logger

Licence
GPL-3.0-only
Version
1.0.5
Deps
0
Size
47 kB
Vulns
0
Weekly
0
Stars
5
DeprecatedThis package is deprecated
NPM Discord Apache-2.0

Pretty-print utility logger for JS/TS

@tnotifier/logger provides a small interface on top of the existing console API and includes things such as:

  • Automatic code highlighting via `template literal` syntax
  • High-precision timestamps
  • Namespacing and Instanced Loggers
  • Enable/disable logging at runtime

Preview

Installation

This package is available via NPM:

yarn add @tnotifier/logger

# or

npm install @tnotifier/logger

Usage

By default, @tnotifier/logger exports traditional logging methods using a shared logger instance. For example:

import { debug, error } from '@tnotifier/logger';

try {
    await something();
    debug('success!');
} catch (e) {
    error('failure!', e);
}

However, if you'd like to add namespaces and use multiple logger instances within your application, you can instead instantiate a new Logger:

import { createLogger } from '@tnotifier/logger';

const loggerA = createLogger({
    name: 'module-a',
    color: '#FF0000',
});
const loggerB = createLogger({
    name: 'module-b',
    color: '#00FF00',
});

loggerA.debug('hello world A!');
loggerA.error('hello world B!');

You can also set a global namespace for all Logger instances created via your App. This can be useful if your console displays output from multiple systems, for example an Electron App or when using iFrames.

import { setNamespace } from '@tnotifier/logger';

setNamespace({
    name: 'website',
    color: '#0000FF',
});

This will be pre-pended to all logging that passes through @tnotifier/logger, regardless of whether it was instanced.

To-do

  • Remove side effects to enable tree shaking

Keywords