Source: flatlog.js

/**
flatlog module. it exports constructor of FlatLog

@exports flatlog
@author kevin leptons <kevin.leptons@gmail.com>
@example
const flatlog = requier('./lib/flatlog')

const LogLevel = flatlog.LogLevel

var config = {verbose: tue, 'gwisp'}
var logger = new flatlog(config)

// quick use
logger.info('start new day')
logger.notice('work done', 'kevin')
logger.error('go to workspace')

// other ways to use
logger.log(LogLevel.INFO, 'start new day')
logger.log(LogLevel.NOTICE, 'work done')
logger.log(LogLevel.ERROR, 'go to workspace')
*/
module.exports = flatlog

function flatlog(config) {
  return new FlatLog(config)
}

const chalk = require('chalk')
const columnify = require('columnify')

/**
Log level

@memberof module:flatlog
@readonly
@enum {number}
*/
const LogLevel = {
  /** system is unusable */
  EMERGENCY: 0,

  /** action must be taken immediately */
  ALERT: 1,

  /** the system is in critical condition */
  CRITICAL: 2,

  /** error condition */
  ERROR: 3,

  /** warning condition */
  WARNING: 4,

  /** a normal but significant condition */
  NOTICE: 5,

  /** a purely informational message */
  INFO: 6,

   /** messages to debug an application */
  DEBUG: 7
}
flatlog.LogLevel = LogLevel

/**
Configuration of logger

@memberof module:flatlog
@typedef {object} Config
@property {boolean} [config.verbose=true] Decide log message or not
@property {string} [config.caller=anonymous] Default caller will be write
@example
var config = {
  verbose: true,
  'gwisp'
}
*/

/**
Create new FlatLog from configuration. It help write log message simply
and readable

@constructor
@memberof module:flatlog
@class FlatLog
@param {module:flatlog.Config} [config={}] - configuration of logger
@return {FlatLog} the new flat log object
@example
var logger1 = new FlatLog()
var logger2 = new FlatLog({caller: 'kevin'})
var logger3 = new FlatLog({verbose: false, caller: 'stuart'})
*/
function FlatLog (config) {
  /**
  Logger configuration. It assign by config parameter

  @member {module:flatlog.Config}
  */
  this._config = config || {}
  if (this._config.verbose === undefined) {
    this._config.verbose = true
  }
  if (!this._config.caller) {
    this._config.caller = chalk.red('anonymous')
  }
}
const prototype = FlatLog.prototype

/**
Log message to terminal by one line format

@memberof! module:flatlog.FlatLog
@param {module:flatlog.LogLevel} level - Level of message
@param {string} message - Message to write
@example
// see constructor for detail
var logger = new FlatLog(config)

logger.log(LogLevel.INFO, 'start new day')
logger.log(LogLevel.NOTICE, 'work done')
logger.log(LogLevel.ERROR, 'go to workspace')
*/
prototype.log = function (level, message) {
  const self = this

  // do not thing if configuration say not verbose
  if (!self._config.verbose) {
    return
  }

  // chose format for each log level
  // convert log level from integer to string
  var format
  var levelName
  switch (level) {
    case LogLevel.EMERGENCY:
      format = chalk.black
      levelName = 'EMERGENCY'
      break
    case LogLevel.ALERT:
      format = chalk.red
      levelName = 'ALERT'
      break
    case LogLevel.CRITICAL:
      format = chalk.green
      levelName = 'CRITICAL'
      break
    case LogLevel.ERROR:
      format = chalk.yellow
      levelName = 'ERROR'
      break
    case LogLevel.WARNING:
      format = chalk.blue
      levelName = 'WARNING'
      break
    case LogLevel.NOTICE:
      format = chalk.magenta
      levelName = 'NOTICE'
      break
    case LogLevel.INFO:
      format = chalk.cyan
      levelName = 'INFO'
      break
    case LogLevel.DEBUG:
      format = chalk.gray
      levelName = 'DEBUG'
      break
    default:
      throw new Error('log level is invalid')
  }

  // create data of message
  var data = [{
    level: format.bold(levelName),
    message: message,
    caller: self._config.caller
  }]

  // format data
  var line = columnify(data, {
    showHeaders: false,
    truncate: true,
    config: {
      level: {
        minWidth: 11,
        maxWidth: 11
      },
      message: {
        minWidth: 56,
        maxWidth: 56
      },
      caller: {
        minWidth: 10,
        maxWidth: 10,
        align: 'right'
      }
    }
  })

  console.log(line)
}

/**
Log message with EMERGENCY level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.emergency('system is unusable')
*/
prototype.emergency = function (message) {
  this.log(LogLevel.EMERGENCY, message)
}

/**
Log message with ALERT level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.alert('action must be taken immediately')
*/
prototype.alert = function (message) {
  this.log(LogLevel.ALERT, message)
}

/**
Log message with CRITICAL level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.critical('the system is in critical condition')
*/
prototype.critical = function (message) {
  this.log(LogLevel.CRITICAL, message)
}

/**
Log message with ERROR level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.error('error condition')
*/
prototype.error = function (message) {
  this.log(LogLevel.ERROR, message)
}

/**
Log message with WARNING level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.warning('warning condition')
*/
prototype.warning = function (message) {
  this.log(LogLevel.WARNING, message)
}

/**
Log message with NOTICE level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.notice('a normal but significant condition')
*/
prototype.notice = function (message) {
  this.log(LogLevel.NOTICE, message)
}

/**
Log message with INFO level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.info('a purely informational message')
*/
prototype.info = function (message) {
  this.log(LogLevel.INFO, message)
}

/**
Log message with DEBUG level

@memberof module:flatlog.FlatLog
@param {string} message log message
@example
// see constructor for detail
var logger = new Logger(config)

logger.debug('messages to debug an application')
*/
prototype.debug = function (message) {
  this.log(LogLevel.DEBUG, message)
}