quark-log

1.2.1 • Public • Published

quark-log

build status stability npm version js-standard-style semantic-release

Simple configurable logging module.

This package is part of quark framework but it can be used independently.

Preview

Features

  • Levels : Defaults (log, info, warn, error) and custom
  • Plugins : Defaults and custom
  • Styling : Per level/plugin
  • More : Timer, group, reset...

Installation

NPM

npm install quark-log --save

Usage

Basic

import { Logger } from 'quark-log'
 
const logger = new Logger()
 
logger.log('Test')
logger.log('Test', { test: true })
logger.log('Test', 10, true)

Enable/disable logging

import { Logger } from 'quark-log'
 
const logger = new Logger()
 
logger.off()
logger.log('This test will not be logged')
logger.on()

Grouping

import { Logger } from 'quark-log'
 
const logger = new Logger()
 
logger.group('Group')
logger.log('Test grouping')
logger.groupEnd()

Timer

import { Logger } from 'quark-log'
 
const logger = new Logger()
 
logger.time('timer')
// ...
logger.timeEnd('timer')

Handler

import { Logger } from 'quark-log'
 
const logger = new Logger()
 
logger.setHandler(msgs => {
  // msgs = ['Test', true]
})
 
logger.log('Test', true)

Reset

Reset levels and plugins.

import { Logger, plugins } from 'quark-log'
 
const logger = new Logger()
logger.plugin('time', plugins.time)
 
logger.log('Test') // [15:00:00] Test
logger.reset()
logger.log('Test') // Test

Line break

import { Logger } from 'quark-log'
 
const logger = new Logger()
logger.br()

Levels

Defaults levels

import { Logger } from 'quark-log'
 
const logger = new Logger()
logger.log('Log')
logger.info('Info')
logger.warn('Warn')
logger.error('Error')

Add a custom level

import { Logger } from 'quark-log'
 
const logger = new Logger()
logger.level('custom')
 
logger.custom('My custom level')

Configure level options

import { Logger } from 'quark-log'
 
const logger = new Logger()
logger.level('custom', {
  styles: {
    color: 'blue'
  }
})
 
logger.custom('Test') // Will be logged in blue

Plugins

Defaults plugins list

  • time : Append current time before user message
  • namespace : Append a custom namespace before user message

Use a default plugin

import { Logger, plugins } from 'quark-log'
 
const logger = new Logger()
logger.plugin('time', plugins.time)
 
logger.log('Test') // [15:00:00] Test

Configure plugin options

import { Logger, plugins } from 'quark-log'
 
const logger = new Logger()
logger.plugin('namespace', plugins.namespace, { name: 'test' })
logger.log('Test') // [test] Test
 
logger.plugin('namespace', { enabled: false })
logger.log('Test') // Test

Create a custom plugin

import { Logger } from 'quark-log'
 
/**
 * Plugin callback signature
 * 
 * @param {Array} msgs User messages
 * @param {Object} options Plugin options
 * @param {Object} level Level
 * @param {String} level.name Level name
 * @param {Object} level.options Level options
 */
const myPlugin = (msgs, options, level) => {
  return { before: `${options.myOption}:`, msgs, after: '', styles: {} }
}
 
const logger = new Logger()
logger.plugin('myPlugin', myPlugin, { myOption: 'My plugin' }) // This plugin append a custom string before each message
 
logger.log('Test') // My plugin: Test

API

See https://fm-ph.github.io/quark-log/

Build

To build the sources with babel in ./lib directory :

npm run build

Documentation

To generate the JSDoc :

npm run docs

To generate the documentation and deploy on gh-pages branch :

npm run docs:deploy

To serve the JSDoc :

npm run docs:serve

Testing

To run the tests, first clone the repository and install its dependencies :

git clone https://github.com/fm_ph/quark-log.git
cd quark-log
npm install

Then, run the tests :

npm test

To watch (test-driven development) :

npm run test:watch

For coverage :

npm run test:coverage

License

MIT License © Patrick Heng Fabien Motte

Package Sidebar

Install

npm i quark-log

Weekly Downloads

4

Version

1.2.1

License

MIT

Last publish

Collaborators

  • fm_ph