Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including adsl with all npm packages installed. Try it out:

var adsl = require("adsl")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

adsl v0.2.0

Another dead simple logger

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

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free