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:
- Node >= v8.0.0
Reference
Example
const log = try process // init our express app, router, graphql... server catch err log
API
The exported module is a singleton of lib/Log
class. It has the following properties and methods:
log.level
string
- Defaultdebug
- A string to specify the current log level of the instance. It should be one of
debug
,info
,warn
,error
orfatal
.
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
- Default3
- 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 thestacktrace
package- A function to filter the stack layer.
log.filenameFn
Function
- Default from thestacktrace
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 callprocess.exit(1)
to quit the current process afterwards. - Parameters:
f
:string|any
- Optional- The format pattern. See Node
util.format
API for detail.
...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
:string|any
- Optional- The format pattern. See Node
util.format
API for detail.
...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 - Defaulterror
- A string to specify the level to log.
License
MIT