
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
koa-mongodb-logger
Advanced tools

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
})
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.
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>
}
}
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',
]
A wrapper around collection.update. Changes is the document object.
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')
}
}
A wrapper around this.log.update() and $push. Very similar to this.log.set().
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:
startend - req.on('end')header - res.writeHead()finish - res.on('finish')close - res.on('close')error - listens to res, req, socket, and appCreates 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>
}
}
The MIT License (MIT)
Copyright (c) 2014 Jonathan Ong me@jongleberry.com
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.
FAQs
Log and profile Koa requests to MongoDB
We found that koa-mongodb-logger demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.