0.17.0 • Published 11 months ago

@yunnysunny/request-logging v0.17.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

request-logging

Print the express request log to console or save it to external data source by providing onReqFinished callback function.

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').default;

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
req_idStringThe unique id for one log record, can been changed by given genId function.
domainStringThe domain of current server.
original_urlStringThe original url contains query string.
pathStringThe request path doesn't contain query string.
routerStringThe request router for express.
user_agentStringThe user agent.
custom_headersObjectThe specific headers you wanna save.
custom_envsObjectThe specific env variables you wanna save.
methodStringThe http request method.
ipStringThe client's ip.
server_ipStringThe server's ip.
server_hostStringThe server's hostname.
durationNumberThe millisecond the request costed.
pidNumberThe server's process id.
req_seqNumberThe 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 been get from the response header of res-code or res.locals._res_code.
res_dataString/anyThe response data, which will been get from res.locals._res_data.
req_timeNumberThe timestamp of begin time of current request occured.
req_time_stringStringThe time of begin time, formatted in ISO 8601 Extended Format.
req_dataString/anyThe 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;

Breaking changes

0.17.x

  1. req_id is now an unique string, the original req_id has renamed to req-seq.
  2. host is renamed to server_ip.
  3. hostname is renamed to domain.

0.16.x

  1. Remove kafkaSchedule mongooseModel alarm option, please use onReqFinished instead.
  2. The default dataFormat function will use JSON.stringify to return string, since it's safe for elasticsearch. If you want to return with you own format, please pass the dataFormat parameter yourself.

License

MIT

0.17.0

11 months ago

0.16.0

11 months ago

0.16.1

11 months ago

0.16.2

11 months ago

0.13.0

3 years ago

0.14.0

3 years ago

0.15.0

3 years ago

0.15.1

3 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.1

5 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.9.0

6 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago