JSPM

  • Created
  • Published
  • Downloads 3246
  • Score
    100M100P100Q123145F
  • License MIT

🚀 lightweight logger • supports level based filtering and tagging • weighs in at 400 bytes

Package Exports

  • missionlog

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (missionlog) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

missionlog NPM version Build status Coverage Status Bundlephobia dependencies Status

Lightweight logger with an extensible configuration. Supports level based filtering and tagging. Filtering keeps your logs readable and uncluttered while tagging makes them searchable.

Features

  • Small footprint, around 400 bytes with 0 dependencies
  • JSON configuration
  • Filter by level, ERROR > WARN > INFO
  • Filter by tag, 'security' | 'whatever'
  • Log callback is extensible from console to cloud
    • Style terminal output with chalk
    • Send JSON to a cloud service like Loggly
    • Log strings and objects to the console
    • Dynamically combine any of the above or try something new
  • API mirrors console.log, logs objects and supports rest parameters
  • Works reliably with node or any browser through a bundler
  • Includes TypeScript definitions so no need for external @types

Example

log.error('security', 'not authorized', statusCode);
log.warn('transporter', 'Evil twin detected!');

Install

npm install missionlog

Initialize

Tags typically refer to a subsystem or component like 'security' or FooBar.name.When missionlog is initialized, tags can be assigned a level. When a message's level is greater than or equal to its tag's assigned level, missionlog executes a callkack. This simple and elegant approach to logging is incredibly flexible. For example, with missionlog you can seamlessly migrate from console logging to a sophisticated cloud service with minimal impact!

// var log = require('missionlog').log;
improt { log } from 'missionlog';

/**
 * init
 * @param config JSON object which assigns tags levels. If uninitialized,
 *    a tag's level defaults to INFO where ERROR > WARN > INFO.
 * @param callback? supports logging whatever way works best for you
 * @return {Log} supports chaining
 */
log.init(
  { loader: 'INFO', security: 'ERROR', system: 'OFF' },
  (level, tag, msg, params) => {
    const prefix = `${level}: [${tag}] `;
    switch(level) {
      case 'ERROR':
        console.error(prefix, msg, ...params);
        break;
      case 'WARN':
        console.warn(prefix, msg, ...params);
        break;
      case 'INFO':
        console.info(prefix, msg, ...params);
        break;
    }
  });

Usage

// filtered since security's log level ERROR is greater than INFO
log.info('security', 'login successful');

// filtered since system's level is OFF
log.error('system', 'eject the warp core', error);

// updates tag levels
log.init({ loader: 'ERROR', system: 'INFO' });