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

- 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:
Reference
Example
const log = require('nn-node-log')
try {
process.on('uncaughtException', (err) => {
log.stack(err)
})
server.listen(8080, () => log.info('listening on port 8080'))
} catch (err) {
log.stack(err, 'fatal')
}

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:
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