1.0.2 • Published 1 year ago
express-error-logging-middleware v1.0.2
express-error-logging-middleware
npm package for express error logging middleware, storing errors in server and pin-pointing the troubled api.
- It is a middleware, compatible with express framework, needs to be placed as middleware in route function after the controller call.
- Stores all 4XX and 5XX errors on server that are passed as next(err) or next(new Error()) way.
- And helps into troubleshooting apis by finding which api has logged most errors.
How to pass as middleware?
const { errorLoggingMiddleware } = require("express-error-logging-middleware");
getRoutes(){
this.route.get("/get", (req, res, next) => {
this.controller.someController(req, res, next)
},errorLoggingMiddleware);
}
It creates two .txt files, (err-400.txt and err-500.txt) in root directory, to store 4XX and 5XX errors respectively.
How to pass err in next()
next(error);
or
next(new Error("err"))
- error can be array or string
Functions
1
getLatestErrors(statusType, countFromLast)
statusType can be 4XX or 5XX countFromLast is an integer.
eg. getLatestErrors(404, 10); Will return the list of lastest 10 errors of statusCode 4XX, that has occured in the application.
2
getApiWithMostError(statusType)
statusType can be 4XX or 5XX
eg. getApiWithMostError(500); Will return the api name and its error count of type 5XX. For example 'v1/api/get', 20 means, 'v1/api/get' api encountered 5XX error the highest number of times compared to any other api, and the count is 20.