npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

morgan-mongo

v1.1.0

Published

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

Downloads

12

Readme

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

{
    "_id" : ObjectId("5c012d5375bad213309ad4c3"),
    "userAgent" : {
        "family" : "Chrome",
        "major" : 70,
        "minor" : 0,
        "patch" : 3538,
        "source" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
    },
    "date" : ISODate("2018-11-30T12:30:11.814Z"),
    "httpVersion" : "1.1",
    "method" : "GET",
    "remoteAddr" : "::1",
    "responseTime" : 92.56,
    "status" : 200,
    "url" : "/quotes?date=2018-11-28"
}

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