0.3.0 • Published 5 years ago

nn-node-log v0.3.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

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

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.6

5 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago