0.2.2 • Published 5 years ago

logmitter v0.2.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Build Status codecov NPM Version

Logmitter is a universal logging utility which acts as event emitter. It's a lightweight open-source package for the server and browser (using module bundler), written with TypeScript. It's actively maintained, well tested and already used in production environments. The source code is available on GitHub where you can also find our issue tracker.

Installation

Run the command below to install the package.

$ npm install --save logmitter

Usage

Logger class is an event emitter which provides standard methods for triggering and listening to different logging events. This class should be used as a central logger instance of your application.

Listening and triggering events

We create a simple logger by creating a new instance of a Logger class. We can then emit and listen to different logging events.

import { Logger, LogEvent } from 'logmitter';

const logger = new Logger(namespace);

logger.on(LogEvent.INFO, function({ date, event, message, namespace }) {
  console.info(message);
});

logger.info('Hello world!');

Custom message format

We can easily change the message format by setting a custom message interface.

interface Message {
  code: number;
  message: any;
};

const logger = new Logger<Message>(namespace);

logger.warn({ code: 200, message: 'Hello world!' });

Namespaced logger

To distinguish between application parts, you can namespace the logger instance by creating a child instance which passes all events to the master instance.

const userLogger = logger.child('user');

API

Logger(namespace)

Main logger class.

OptionTypeRequiredDefaultDescription
namespaceStringNo-Logger namespace.

Logger.prototype.child(namespace): LoggerChild

Returns logger child instance.

OptionTypeRequiredDefaultDescription
namespaceStringNo-Logger namespace.

Logger.prototype.debug(message): Boolean

Triggers debug event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

Logger.prototype.emit(event, message, namespace): Boolean

Triggers log event.

OptionTypeRequiredDefaultDescription
eventStringYes-Event name.
messageMessageYes-Event message.
namespaceStringNothisLogger namespace.

Logger.prototype.error(message): Boolean

Triggers error event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

Logger.prototype.info(message): Boolean

Triggers info event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

Logger.prototype.namespace: string

Returns logger namespace.

Logger.prototype.off(event, resolver): Logger

Removes attached event listener.

OptionTypeRequiredDefaultDescription
eventStringYes-Event name.
resolverFunction, PromiseNo-Event resolver function. All event with the provided name are removed when the resolver is not provided.

Logger.prototype.on(event, resolver): Logger

Attaches a listener function to log event.

OptionTypeRequiredDefaultDescription
eventStringYes-Event name.
resolverFunction, PromiseYes-Event resolver function.

Logger.prototype.once(event, resolver): Logger

Attaches a listener function to log the event and automatically removes it after the event is triggered.

OptionTypeRequiredDefaultDescription
eventStringYes-Event name.
resolverFunction, PromiseYes-Event resolver function.

Logger.prototype.warn(message): Boolean

Triggers warn event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

LoggerChild(parent, namespace)

Logger child class.

OptionTypeRequiredDefaultDescription
parentLoggerYes-Parent logger class instance.
namespaceStringYes-Logger namespace.

LoggerChild.prototype.child(namespace): LoggerChild

Returns logger child instance.

OptionTypeRequiredDefaultDescription
namespaceStringNo-Logger namespace.

LoggerChild.prototype.debug(message): Boolean

Triggers debug event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

LoggerChild.prototype.error(message): Boolean

Triggers error event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

LoggerChild.prototype.info(message): Boolean

Triggers info event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

LoggerChild.prototype.namespace: string

Returns logger namespace.

LoggerChild.prototype.warn(message): Boolean

Triggers warn event.

OptionTypeRequiredDefaultDescription
messageMessageYes-Event message.

Available events

EventValueDescription
LogEvent.ERRORerrorError event.
LogEvent.WARNwarnWarning event.
LogEvent.INFOinfoInfo event.
LogEvent.DEBUGdebugDebug event.

Contributing

See CONTRIBUTING.md for how to help out.

Licence

See LICENSE for details.