Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @yunnysunny/request-logging with all npm packages installed. Try it out:

var requestLogging = require("@yunnysunny/request-logging")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@yunnysunny/request-logging v0.12.0

Print the express request log to console and save it to kafka when required, and even can send alram message when the response code greater than 500.

request-logging

Print the express request log to console and save it to kafka and mongodb when required, and even can send alram message when the response code greater than 500.

npm version build status David deps Test coverage node version

Installation

npm install @yunnysunny/request-logging --save

Usage

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const requestLog = require('@yunnysunny/request-logging');

const app = express();
app.enable('trust proxy');

// view engine setup
app.set('port', port);
app.use(requestLog());

app.use(bodyParser.json({limit: '1mb'}));
app.use(bodyParser.urlencoded({
    extended: false,
    limit: '1mb'
}));

API

See api document.

Fields

If you want to save request logging to mongodb, this is the fields description, which you will used to create a mongoose schema:

nametypedescription
hostnameStringThe domain of current server.
original_urlStringThe original url contains query string.
pathStringThe request path doesn't contain query string.
user_agentStringThe user agent.
custom_headersObjectThe specific you wanna save.
methodStringThe http request method.
ipStringThe client's ip.
hostStringThe server's ip.
durationNumberThe millisecond the request costed.
pidNumberThe server's process id.
req_idNumberThe inner request number, auto increased when new request come.
content_length_reqNumberThe content-length of the request headers.
content_lengthNumberThe content-length of the response headers.
status_codeNumberThe status code of current HTTP response.
res_codeNumberThe inner response code, which will be got from the response header of res-code.
res_dataObjectThe reponse data.
req_timeNumberThe timestamp of begin time of current request occured.
req_time_stringStringThe time of begin time, formated in ISO 8601 Extended Format.
req_dataObjectThe request data, which would form query string or form data.
refererStringThe HTTP referer header.
sessionObjectThe session of current request.
abortedBooleanWhether the request has aborted.

We suggest you use such mongoose schema, which is compatible when the fields is changed:

const {Schema} = require('mongoose');

const requestLogSchema =  new Schema({
    req_time: Date
},{ 
    timestamps: {
        createdAt: 'created_at',
        updatedAt : 'updated_at'
    },
    strict: false
});
module.exports = requestLogSchema;

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free