abslog
TypeScript icon, indicating that this package has built-in type declarations

2.4.4 • Public • Published

abslog

Dependencies GitHub Actions status Known Vulnerabilities

An abstract logger - Enables adding logging to modules without adding a dependency to a full log library. This way, the consumer of your modules can plug in their own log library of choice.

The API of this abstract logger conforms to the standard Log4j interface levels.

Installation

$ npm install abslog

Usage

In your module(s):

const abslog = require('abslog');

class AwesomeModule {
    constructor(logger) {
        this.log = abslog(logger);
    }

    doStuff() {
        this.log.info('Did stuff');
    }
}

The consumer of your module, using pino as logger:

const AwesomeModule = require('AwesomeModule');
const pino = require('pino')();

const awesome = new AwesomeModule(pino);
awesome.doStuff()  // will now log on info

Interface

abslog takes one argument. It takes an logger expected to be log4j interface compatible.

If no logger is provided an object with log methods will be returned. Each of these method are just noop methods adding minimal footprint and performance impact on your module.

If a logger is provided, the logger will be returned. This leaves abslog adding zero runtime footprint and performance impact on your module.

If an logger is provided but the logger is not compatible with the log4j interface, an exception will be thrown.

Methods

The log4j compatilbe methods are:

  • fatal
  • error
  • warn
  • info
  • debug
  • trace

Example:

const abslog = require('abslog');

const log = abslog();
log.fatal('critital message');
log.error(new Error(), 'error message');
log.warn('warn message');
log.info('info message');
log.debug('debug message');
log.trace('trace message');

console.log

Console is not log4j compatible, but abslog does support Console for logging. Its worth noticing that its not recommended to use Console for logging in production code, but it can be handy to just plug in Console to get log output in development.

Example:

const abslog = require('abslog');

const log = abslog(console);
log.info('some message');
log.fatal('critical message');

Known compatible loggers:

These loggers are known to be compatible and tested:

pino

pino is found to be compatible.

const abslog = require('abslog');
const pino = require('pino')();

const log = abslog(pino);
log.info('hello');

bunyan

bunyan is found to be compatible.

const abslog = require('abslog');
const bunyan = require('bunyan');

const logger = bunyan.createLogger({
    name: 'test',
    stream: process.stdout,
    level: 'trace'
});

const log = abslog(logger);
log.info('hello');

log4js

log4js is found to be compatible.

const abslog = require('abslog');
const log4js = require('log4js');

const logger = log4js.getLogger();

const log = abslog(logger);
log.info('hello');

winston

winston is found to NOT be compatible out of the box. Though, winston can be customised with alternative log levels and through such a configuration be compatible.

const abslog = require('abslog');
const winston = require('winston');

const winstonCustomLevels = {
    levels: {
        fatal: 0,
        error: 1,
        warn: 2,
        info: 3,
        debug: 4,
        trace: 5,
    },
    colors: {
        fatal: 'red',
        error: 'red',
        warn: 'yellow',
        info: 'green',
        debug: 'blue',
        trace: 'cyan',
    }
};

const logger = winston.createLogger({
    levels: winstonCustomLevels.levels,
});

const log = abslog(logger);
log.info('hello');

roarr

roarr is found to be compatible.

const abslog = require('abslog');
const roarr = require('roarr').default;

const log = abslog(roarr);
log.info('hello');

synclog

synclog is found to be compatible.

const abslog = require('abslog');
const SyncLog = require('synclog')

const log = abslog(new SyncLog('debug'));
log.info('hello');

LogDna

LogDna is found to be compatible.

const abslog = require('abslog');
const LogDna = require('logDna')

const log = abslog(Logdna.createLogger('some_api_key_goes_here', {}));
log.info('hello');

License

The MIT License (MIT)

Copyright (c) 2018 - Trygve Lie - post@trygve-lie.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i abslog

Weekly Downloads

2,245

Version

2.4.4

License

MIT

Unpacked Size

11.7 kB

Total Files

7

Last publish

Collaborators

  • trygve-lie