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

koa-mongodb-logger

v0.2.0

Published

Log and profile Koa requests to MongoDB

Downloads

20

Readme

Koa MongoDB Logger Build Status

Log and profile your Koa requests to MongoDB.

var koa = require('koa')
var logger = require('logger')(app, collection)

var app = koa()
app.use(logger)

app.use(function* (next) {
  yield User.get(this)
  this.log.emit('user') // profile
  this.log.set('user', { // logging
    _id: user._id,
    name: user.name
  })
  yield next
})

API

var logger = Logger(app, [collection])

Creates a logging middleware.

You can also set the collection asynchronously by doing:

logger.collection = collection

Just make sure you don't start serving until the collection is set.

For performance, you may want to set w: 0 on your collection since data loss doesn't matter much here.

this.log

The log is stored as this.log. This is not the document itself as stored in MongoDB, but an interface to push data. If you want the actual document, you'll have to query MongoDB yourself.

The structure of the log document is:

{
  request: this.request,
  response: this.response,
  errors: [],
  event: {
    <event>: <date>
  }
}

this.log.requestProperties, this.log.responseProperties

Arrays of properties to save from this.request and this.response, respectively. You can set these yourself. By default, they are:

Logger.prototype.requestProperties = [
  'header',
  'method',
  'url',
  'path',
  'querystring',
  'query',
  'host',
  'protocol',
  'secure',
  'ip',
  'ips',
]

Logger.prototype.responseProperties = [
  'header',
  'status',
]

this.log.update(changes, [callback])

A wrapper around collection.update. Changes is the document object.

this.log.set(key, value, [callback])

A wrapper around this.log.update() and $set. Sets a key/value. For example:

this.set('user._id', user._id)

Would make the log look something like:

{
  request: this.request,
  response: this.response,
  errors: [],
  event: {
    <event>: <date>
  },
  user: {
    _id: ObjectId('123412341234123412341234')
  }
}

this.log.push(key, value, [callback])

A wrapper around this.log.update() and $push. Very similar to this.log.set().

this.log.emit(event, [error])

Log isn't an EventEmitter - emit is special. It takes no additional arguments except error if and only if event === 'error'.

This is for profiling events in your request. Events are saved as <event>: <date>. For example:

this.log.emit('user')

Will create a log that looks like:

{
  event: {
    start: <some date>,
    user: <some date + 10ms>
  }
}

Thus, do not use duplicate events!

The following events are emitted and saved automatically:

  • start
  • end - req.on('end')
  • header - res.writeHead()
  • finish - res.on('finish')
  • close - res.on('close')
  • error - listens to res, req, socket, and app

this.log.bind(event)

Creates a listener for a .on() event. Example:

var stream = fs.createReadStream()

stream.on('finish', this.log.bind('when this stream finishes'))

will add a log:

{
  event: {
    'when this stream finishes': <date>
  }
}

License

The MIT License (MIT)

Copyright (c) 2014 Jonathan Ong [email protected]

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.