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-persist

v1.0.5

Published

Logs persistance layer using Morgan and MongoDB

Downloads

26

Readme

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:[email protected]: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