NodeJS Request Logger
This module creates a middleware for request logging.
This logs request url, status and response time to mysql database using sequelize on response finish event without affecting api performance.
Additional properties user_id: req.user.id
Prerequisites
Thins middleware need time taken by request to complete.
- Response Time npm install --save response-time
MYSQL Table Schema (table name is configurable)
NOT EXISTS `access_logs` ( `id` int(11) NOT NULL, `response_time` decimal(15,4) DEFAULT NULL, `status` int(11) DEFAULT NULL, `request` varchar(255) DEFAULT NULL, `method` varchar(255) DEFAULT NULL, `user_id` int(14) DEFAULT NULL, `app_id` tinyint(3) unsigned DEFAULT NULL, `created_on` datetime DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; `access_logs` ADD PRIMARY KEY (`id`); `access_logs` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
Installation
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install express-request-logging
API
const requestLogger =
requestLogger(db, config, callback)
Create a middleware that logs request url, status and response time on response finish event.
db
Its sequelize database instance in which access_logs
table is present.
config
You can override default table name and pass application id.
{
appId: number, // this is used if multiple application storing request logs in same database (default: null)
tableName: string, // mysql table name (default: 'access_logs')
}
callback
Callback will be called in case of any error
Example
Default
const responseTime = const requestLogger = appapp
Custom
const responseTime = const requestLogger = appapp