@inveris/dev-logger
TypeScript icon, indicating that this package has built-in type declarations

2.0.9 • Public • Published

dev-logger

npm version

A ESM console logger for development purpose with human readable output and easy modifiability.

Log levels can be customized, as well as the output (via formatter methods). View examples/ directory.

Please do not use this logger in production mode! For production use pino, winston, bunyan, ...

Install

npm install --save-dev @inveris/dev-logger

Usage

import DevLogger from '@inveris/dev-logger'

const log = new DevLogger(__filename)

log.trace('trace message')
log.debug('debug message')
log.info('info message')
log.success('success message')
log.warn('warn message')
log.error('error message')
log.fatal('fatal message')

const str = 'some value'
log.info('with a string', str)

const obj = {
  value1: 'abc',
  value2: 123
}
log.info('with an object', obj)

const arr = [ 'a', 'b', 4711 ]
log.info('with an array', arr)

Options

const log = new DevLogger(options)

Options could be a string (group) or an object with the following settings:

  • group {string} Name of the group.
    A group is a coherent block of messages that belong together.
    Simpelst usage is setting the current filename.

    group: __filename
  • name {string} An identifier at each line.
    E.g. the application name from package.json could be used.

  • logLevel {number|string} The level from where the output is visible.

  • upperCaseLevelName {boolean} Should the level name displayd in upper case. Default true.

  • padStartLevelName {boolean} Should the level name padded before. Default false.

  • padEndLevelName {boolean} Should the level name padded after. Default false.

  • withDate {boolean} Should the date be visible. Default false.

  • withGroup {boolean} Should the group be visible. Default true.

  • withName {boolean} Should the name be visible. Default true.

  • colors {object} Colors of the output.
    View index.js defaultColors

    colors: {
      trace: 'green',
      info: 'blue'
    }
  • levels {object} List of log methods.
    View index.js defaultLevels

    levels: {
      10: 'debug',
      20: 'info',
      30: 'warn warning',
      40: 'error'
    }

    In this example, we had only debug, info, warn, warning and error methods.
    The first string is the prefix, that is visible on output, e. g. WARN message will be displayed with log.warn('message') or log.warning('message')

Methods

  • setColors(colors)

  • setGroup(group)

  • setName(name)

  • setLevels(levels)

  • setLogLevel(level)

    • level {number|string} The level from where the output is visible.

Default Log-Levels

  • trace
  • debug
  • info
  • success
  • warn / warning
  • error
  • fatal

Custom Log-Levels

import DevLogger from '@inveris/dev-logger'

const log = new DevLogger({
  levels: {
    100: 'basic',
    200: 'normal',
    300: 'extended'
  },
  colors: {
    basic: 'grey',
    normal: 'green',
    extended: 'yellow'
  }
})

log.basic('basic message')
log.normal('normal message')
log.extended('extended message')

Custom formatter

View examples/custom-formatter.js.

Examples

More examples in examples/ directory.

Example:

import DevLogger from '@inveris/dev-logger'

const log = new DevLogger('some id')

log.setLogLevel('trace')

log.trace('trace message')
log.debug('debug message')
log.info('info message')
log.success('success message')
log.warn('warn message')
log.error('error message')
log.fatal('fatal message')

Package Sidebar

Install

npm i @inveris/dev-logger

Weekly Downloads

0

Version

2.0.9

License

MIT

Unpacked Size

23.1 kB

Total Files

8

Last publish

Collaborators

  • rottmann