1.0.5 • Published 2 years ago

@tnotifier/logger v1.0.5

Weekly downloads
-
License
GPL-3.0-only
Repository
github
Last release
2 years ago

@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