1.0.5 • Published 4 years ago

morgan-persist v1.0.5

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

Morgan Persist

Logs persistance layer using Morgan and MongoDB

Installation

yarn add morgan-persist

Usage

import express from "express"
import { morganPersist } from "morgan-persist"

const app = express()

app.use(
    morganPersist({
        connectionString: "mongodb://superuser:10moon10@ds151917.mlab.com:51917/testlogger",
    })
)

Output sample

{
    "_id": {
        "$oid": "5ef041bfdabb291193764844"
    },
    "userAgent": {
        "family": "Other",
        "major": 0,
        "minor": 0,
        "patch": 0,
        "source": "insomnia/2020.2.2"
    },
    "date": {
        "$date": "2020-06-22T05:29:35.751Z"
    },
    "httpVersion": "1.1",
    "method": "GET",
    "remoteAddr": "::1",
    "responseTime": 2.27,
    "status": 404,
    "url": "/tasks",
    "authorization": "some token"
}

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.

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

morgan-mongo - not supporting node > 11.x.

License

MIT

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago