This logger decorates logging messages made to it with additional data in JSON format, and if instantiated will monitor long-running requests. To use it, require it, and provide it with the its context's filename if you would like that added to the logging object.
JSON is minified when NODE_ENV === 'production'
To run, clone this repo, npm i
and npm start
.
See examples on localhost:3010
Without instantiation (eg const logger = require('./logger')
, these console methods are supported, as they would normally be used:
error
info
log
warn
After instantiation the following are used
end
(no log recorded, just cleanup)error
info
log
start
success
warn
// require and use in place of console to use:
const logger = require('./logger')
logger.log('simplest use')
// optionally use the `create` method if you would like the log
// to contain the name of the file that generated it:
const loggerInstance = logger.create({
__filename
})
loggerInstance.log('log without instantiation that shows filename')
becomes:
{
"logLevel": "LOG",
"message": "log without instantiation that shows filename'",
"originFile": "/Users/thibbard/Documents/logger/controller.js",
"timestamp": "2018-03-13T21:42:13.686Z"
}
const Logger = new loggerInstance({
fn: 'longRequest' // optionally pass it a function name to be included
}).start('optional message')
axios
.get('https://jsonplaceholder.typicode.com/posts/1')
.then(data => {
Logger.success('optional success message')
res.json(data.data)
})
.catch(error => {
Logger.error(error) // pass the error object
res.json({ error: 'an error occurred' })
})
success output:
{
"timestamp": "2018-03-13T21:53:06.178Z",
"functionName": "longRequest",
"request": {
"start": 1520977986178,
"status": "START",
"timeElapsed": 0
},
"originFilename": "/Users/thibbard/Documents/logger/controller.js",
"logLevel": "INFO"
}
{
"logLevel": "LOG",
"message": "arbitrary bit of data after instantiation",
"originFile": "/Users/thibbard/Documents/logger/controller.js",
"timestamp": "2018-03-13T21:53:06.178Z"
}
{
"timestamp": "2018-03-13T21:53:06.178Z",
"functionName": "longRequest",
"request": {
"start": 1520977986178,
"status": "SUCCESS",
"timeElapsed": 98,
"end": 1520977986276
},
"message": "optional success message",
"originFilename": "/Users/thibbard/Documents/logger/controller.js",
"logLevel": "INFO"
}
error output:
{
"timestamp": "2018-03-13T21:50:48.025Z",
"functionName": "urlFail",
"request": {
"start": 1520977848025,
"status": "START",
"timeElapsed": 1
},
"originFilename": "/Users/thibbard/Documents/logger/controller.js",
"logLevel": "INFO"
}
{
"timestamp": "2018-03-13T21:50:48.025Z",
"functionName": "urlFail",
"request": {
"start": 1520977848025,
"status": "ERROR",
"timeElapsed": 66,
"error": {
"code": "ENOTFOUND",
"hostname": "a-made-up-fictional-url-for-testing.org",
"url": "https://a-made-up-fictional-url-for-testing.org/test/test"
}
},
"originFilename": "/Users/thibbard/Documents/logger/controller.js",
"logLevel": "ERROR"
}
Long requests output first a warning, then after 2x the threshhold, an error
{
"timestamp": "2018-03-13T21:55:05.753Z",
"functionName": "longRequest",
"request": {
"method": "GET",
"originalEndpoint": "/long",
"start": 1520978105753,
"status": "START",
"timeElapsed": 0
},
"message": "starting",
"originFilename": "/Users/thibbard/Documents/logger/controller.js",
"logLevel": "INFO"
}
{
"timestamp": "2018-03-13T21:55:05.753Z",
"functionName": "longRequest",
"request": {
"method": "GET",
"originalEndpoint": "/long",
"start": 1520978105753,
"status": "WARNING_LONG_RUNNING_PROCESS",
"timeElapsed": 1000
},
"originFilename": "/Users/thibbard/Documents/logger/controller.js",
"logLevel": "WARN"
}
{
"timestamp": "2018-03-13T21:55:05.753Z",
"functionName": "longRequest",
"request": {
"method": "GET",
"originalEndpoint": "/long",
"start": 1520978105753,
"status": "SUCCESS",
"timeElapsed": 1501,
"end": 1520978107254
},
"message": "optional message",
"originFilename": "/Users/thibbard/Documents/logger/controller.js",
"logLevel": "INFO"
}