Nice Philanthropist Metalhead
Join us to discuss the challenges, solutions and best practices for in-house JavaScript code sharing. Tuesday, 12/17 at 10am PT/1pm ET.Sign up here »

adsl

0.2.0 • Public • Published

adsl

Another dead simple logger inspired by the awesome console-log-level.

js-standard-style

A dead simple logger. Will log to STDOUT or STDERR depending on the chosen log level. It uses console.info, console.warn and console.error and hence supports the same API.

Log levels sets supported

npm (default)

{ error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }

console-log-level:

{ fatal: 0, error: 1, warn: 2, info: 3, debug: 4 }

rfc5424:

{ emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 }

Installation

npm install adsl

Example usage

// example.js
const adsl = require('adsl')
const colors = require('colors')
const fs = require('fs')
const os = require('os')
 
function colorsTransport (styles, msg, shouldLog, lvl, lvlIndex) {
  if (colors.supportsColor && styles[lvl]) {
    msg = styles[lvl](msg)
  }
  return adsl.defaultTransport(msg, shouldLog, lvl, lvlIndex)
}
 
function streamTransport (wstream, msg, shouldLog, lvl, lvlIndex) {
  wstream.write(`${lvlIndex} ${new Date()} ${msg}${os.EOL}`)
}
 
var logWriteStream = fs.createWriteStream('log.txt')
 
const log = adsl({
  level: 'info',
  prefix(level) {
    return level.toUpperCase()
  },
  transport: [
    streamTransport.bind(null, logWriteStream),
    colorsTransport.bind(null, {
      trace: colors.grey,
      debug: colors.grey,
      info: colors.cyan,
      warn: colors.red,
      error: colors.bgRed,
      fatal: colors.bgRed
    })
  ]
})
 
log.info('current level:', log.level, log.levelIndex)
log.info('visible')
log.debug('invisible')
 
log.level = 'debug'
 
log.info('current level:', log.level, log.levelIndex)
log.info('foo')
log.debug('bar')
 
logWriteStream.end(os.EOL)
 

example output

Options

Configure the logger by passing an options object:

var log = require('adsl')({
  level: 'info',
  levels: 'npm',
  prefix: function (level) {
    return level.toUpperCase()
  },
  defaultTransport: function (msg, shouldLog, lvl, lvlIndex) {
    if (shouldLog) {
      console.log(lvlIndex, lvl, msg)
    }
  }
})

level

A string to specify the log level.

Defaults to : adsl.defaultLevel = "info"

levels

An string to specify the log levels set used.

Defaults to: adsl.defaultLevels = "npm"

prefix

Specify this option if you want to set a prefix for all log messages. This must be a string or a function that returns a string.

transport

Function called by the logger at each logging operation. This must be a function or an array of functions.

Defaults to : adsl.defaultTransport =

function (msg, shouldLog, lvl, lvlIndex) {
  shouldLog &&
    (console[ADSL.outputMap[lvl] || lvl] || console.log).call(console, msg)
}

License

MIT

Install

npm i adsl

DownloadsWeekly Downloads

1

Version

0.2.0

License

MIT

Last publish

Collaborators

  • avatar