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

express-log-mongo

v1.1.2

Published

Express middleware based off morgan to log into mongodb. Simple and effective.

Downloads

9

Readme

express-log-mongo

Library to be used as express middleware logging information and storing the data into a mongo database. The logging information is based off the Morgan package.

Including express-log-mongo

A pre-requisite for this package to work is to have MongoDB already setup and available on the network.

npm install express-log-mongo --save

Usage

Import the express-log-mongo module

const logger = require('express-log-mongo');

API

constructor

logger(format, options)

Create a new express-log-mongo logger middleware function using format and options variables.

Arguments
format

This variable can be a string of a predefined format ('default', 'short', 'tiny') or it can be a string that holds the specific tokens to be stored (:date :method :url :status).

options

This object must consist of a url, db, and collection. These referer to the connection to mongodb url, the database, and the collection. An example of the options object is:

{
    url: 'mongodb://localhost:27017',
    db: 'databaseName',
    collection: 'logs'
}
Example
app.use(logger('tiny', {
    url: 'mongodb://localhost:27017',
    db: 'databaseName',
    collection: 'logs'
}));

retrieveDB

logger.retrieveDB(options)
Arguments
options

This object may consist of find, sort, limit, and skip. The default values for limit is 1000 and skip is 0. An example of the options object is:

{
    find: {},
    sort: { 'date': -1 },
    limit: 100,
	skip: 100
}
Example
app.get('/logs', (req, res) => {
    logger.retrieveDB({
        find: {},
        sort: { 'date': -1 }
    }).then((results) => {
        res.json(results);
    }).catch((err) => res.status(500).json(err));
});

Predefined Formats

There are various pre-defined formats provided:

default
:date :method :url :status :remote-addr :response-time :http-version :remote-user :res[content-length] :referrer :user-agent
short
:remote-addr :remote-user :method :url :http-version :status :res[content-length] :response-time
tiny
:method :url :status :res[content-length] :response-time

Tokens

Pre-defined tokens available.

:date

The current date and time.

:http-version

The HTTP version of the request.

:method

The HTTP method of the request.

:referrer

The Referrer header of the request. This will use the standard mis-spelled Referer header if exists, otherwise Referrer.

:remote-addr

The remote address of the request. This will use req.ip, otherwise the standard req.connection.remoteAddress value (socket address).

:remote-user

The user authenticated as part of Basic auth for the request.

:req[header]

The given header of the request. If the header is not present, the value will be displayed as "-" in the log.

:res[header]

The given header of the response. If the header is not present, the value will be displayed as "-" in the log.

:response-time[digits]

The time between the request coming into morgan and when the response headers are written, in milliseconds.

The digits argument is a number that specifies the number of digits to include on the number, defaulting to 3, which provides microsecond precision.

:status

The status code of the response.

:url

The URL of the request. This will use req.originalUrl if exists, otherwise req.url.

:user-agent

The contents of the User-Agent header of the request.

Examples

Basic Example - default Format

const express = require('express');
const logger = require('express-log-mongo');

const app = express();

app.use(logger('default', {
    url: 'mongodb://localhost:27017',
    db: 'databaseName',
    collection: 'logs'
}));

app.get('/', function (req, res) {
  res.send('hello, world!')
})

app.listen(8080);

Using Specific Logging Tokens

const express = require('express');
const logger = require('express-log-mongo');

const app = express();

app.use(logger(':date :method :url :status :remote-addr :response-time :http-version :remote-user :res[content-length] :referrer :user-agent', {
    url: 'mongodb://localhost:27017',
    db: 'databaseName',
    collection: 'logs'
}));

app.get('/', function (req, res) {
  res.send('hello, world!')
})

app.listen(8080);

Use Custom Token Format

const express = require('express');
const logger = require('express-log-mongo');
const uuid = require('node-uuid');

logger.token('id', (req) => {
	return req.id;
});

const app = express();

app.use(logger(':id :date :method :url :status :remote-addr :response-time :http-version :remote-user :res[content-length] :referrer :user-agent', {
    url: 'mongodb://localhost:27017',
    db: 'databaseName',
    collection: 'logs'
}));

app.use((req, res, next) => {
	req.id = uuid.v4();
	next();
});

app.get('/', function (req, res) {
  res.send('hello, world!')
})

app.listen(8080);

Handling log get api calls

const express = require('express');
const logger = require('express-log-mongo');

const app = express();

app.use(logger('default', {
    url: 'mongodb://localhost:27017',
    db: 'databaseName',
    collection: 'logs'
}));

app.get('/', function (req, res) {
  res.send('hello, world!')
})

app.get('/logs', (req, res) => {
    logger.retrieveDB({
        find: {},
        sort: { 'date': -1 }
    }).then((results) => {
        res.json(results);
    }).catch((err) => res.status(500).json(err));
});

app.listen(8080);

Contributors

Randall Simpson

License

The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong Copyright (c) 2014-2017 Douglas Christopher Wilson Copyright (c) 2018 Randall Simpson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.