1.1.1 • Published 4 years ago

morgan-mongx v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

morgan-mongo

Build Status Known Vulnerabilities

Node.js HTTP request logger middleware for Express with MongoDB as storage; morgan and Mongoose based.

Inspired by Storing Log Data MongoDB article.

Built-in support for string, numeric, date and user agent tokens parsing.

Highly configurable output with meaningful defaults; support for standart mongoose and morgan options.

Output Live Demo

Installation

npm install morgan-mongo --save

NPM

Usage

import { morganMongoMiddleware } from 'morgan-mongo';
const app = express();
...
app.use(morganMongoMiddleware());

Output sample

API

morganMongoMiddleware(options?, connectionOptions?, schemaOptions?, morganOptions?)

options

Custom options to provide MongoDB connection string and to control entries parsing.

export type OptionsType = {
    connectionString?: string;
    includeOnly?: string[];
    exclude?: string[];
    augment?: MappingDescriptor;
    customMapping?: MappingDescriptor;
};

connectionString: mongo connection string, defaults to mongodb://localhost:27017/morgan-mongo. If you use MongoDB Atlas (i.e. connection string with mongodb+srv protocol schema), dbName must be additionally provided in connectionOptions.

Usage samples of other options can be found in tests. MappingDescriptor is described in Custom Mapping section below..

connectionOptions

mongoose ConnectionOptions

schemaOptions

mongoose SchemaOptions

morganOptions

morgan Options

Custom mappings

This section is subject for improvements. Feel free to open issus in case of any questions.

MappingDescriptor

Describes mappings from morgan tokens to properties in mongo document:

export type MappingDescriptor = { 
    [tokenName: string]: MappingMeta<any> 
};

Default mappings are described by defaultMappingDescriptor.

MappingMeta

Describes mapping of single morgan token to property in mongo document:

export type MappingMeta<T> = {
    prop: string,
    type?: typeof mongoose.SchemaType | mongoose.SchemaDefinition,
    params?: any[],
    handler?: Handler<T>
};
  • prop: mongo document property name
  • type: mongoose SchemaType. If handler is not provided explicitly, default type handler will be used to handle String, Number, Dates and custom user agent type. If type is omitted token value will be unchanged string.
  • params: morgan token parameters. In case of having multiple same tokens with different parameters to keep uniqueness of keys in MappingDescriptor, parameters can be passed as part of token name there:
 {
    'req:cache-control': {
        prop: 'cacheControl' // maps 'Cache-Control' request header to cacheControl property in mongo document
    },
    'req:content-type': {
        prop: 'contentType' // maps 'Content-Type' request header to contentType property in mongo document
    }

 }
  • handler: optional custom processing of token value (string) to any desired output type

Contribution

Feel free to contribute by opening issues with any questions, bug reports or feature requests.

Credits

mongoose-morgan - entries are not parsed but stored in mongo as strings.

License

MIT