nn-node-log

0.3.0 • Public • Published

Simple Node log

Simple Node log with levels, color, and log location from file name and line number, inspired from the Golang log package.

NPM version NPM monthly download

  • Caution: This package uses some private APIs from Node, those APIs are not guaranteed to be kept overtime. It's also a new package under heavy developing and has not been well tested yet. If any problem happens, feel free to open an issue.
  • Require:
    • Node >= v8.0.0

Reference

Example

const log = require('nn-node-log')
 
try {
  process.on('uncaughtException', (err) => {
    log.stack(err)
  })
 
  // init our express app, router, graphql...
 
  server.listen(8080, () => log.info('listening on port 8080'))
} catch (err) {
  log.stack(err, 'fatal')
}

Example

API

The exported module is a singleton of lib/Log class. It has the following properties and methods:

log.level

  • string - Default debug
  • A string to specify the current log level of the instance. It should be one of debug, info, warn, error or fatal.

log.debugPrefix

  • string - Default is an empty string
  • A string to specify the debug prefix to filter out the unnecessary debug messages.

log.color

  • A boolean flag to indicate that it should print with color or not. Default is true.

log.skip

  • number - Default 3
  • A number to skip when retrieving the stack trace call layer. We should modify this number when we want to wrap the library inside other function calls.

log.timezone

  • number - Default is the current timezone
  • A number to specify the target timezone to be printed out.

log.setCwd(cwd)

  • Set the current working directory
  • Parameters:
    • cwd:
      • string - Required

log.filterFn

  • Function - Default from the stacktrace package
  • A function to filter the stack layer.

log.filenameFn

  • Function - Default from the stacktrace package
  • A function to get the shortened file name from the stack layer.

log.debug([f, ...args])

log.warn([f, ...args])

log.info([f, ...args])

log.error([f, ...args])

log.fatal([f, ...args])

  • Log with a specified format for each level. With fatal level, it will call process.exit(1) to quit the current process afterwards.
  • Parameters:
    • f:
    • ...args:
      • any - Optional
      • The rest arguments to be formatted with.

log.trace(level[, f, ...args])

  • Print out together with the call layers.
  • Parameters:
    • level:
      • string - Required
      • A string to specify the level to log.
    • f:
    • ...args:
      • any - Optional
      • The rest arguments to be formatted with.

log.stack(err[, level])

  • Pretty print out the error.
  • Parameters:
    • err:
      • Error - Required
      • The error to be printed out.
    • level:
      • string - Optional - Default error
      • A string to specify the level to log.

License

MIT

Package Sidebar

Install

npm i nn-node-log

Weekly Downloads

0

Version

0.3.0

License

MIT

Unpacked Size

8.66 kB

Total Files

9

Last publish

Collaborators

  • namnm