Git Product home page Git Product logo

request-logger's Introduction

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

This middleware need time taken by request to complete.

MYSQL Table Schema (table name is configurable)

CREATE TABLE IF 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;

ALTER TABLE `access_logs`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `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 = require('express-request-logging')

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 = require('response-time')
const requestLogger = require('express-request-logging')

app.use(responseTime())
app.use(requestLogger(sequelizeInstance)

Custom

const responseTime = require('response-time')
const requestLogger = require('express-request-logging')

app.use(responseTime())
app.use(requestLogger(sequelizeInstance, { appId: 1 }, (err) => console.log(err))

License

MIT

request-logger's People

Contributors

anishlushte07 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.