Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding functionality to save logs to mongodb #44

Closed
wants to merge 3 commits into from
Closed

Adding functionality to save logs to mongodb #44

wants to merge 3 commits into from

Conversation

emech-en
Copy link

@emech-en emech-en commented Nov 9, 2014

I think this would be good if add ability to save logs on mongodb

emech-en and others added 3 commits November 9, 2014 13:47
index.js changed due to save logs to mongodb if options peresents
test/test.js changed due to test saving to mognodb functionality
@dougwilson
Copy link
Contributor

No, I'm sorry. This library is made to be generic. In fact, looking at your code, you can do exactly what you want with zero modifications to morgan: just pass in a stream that feeds into your mongodb.

@dougwilson dougwilson closed this Nov 9, 2014
@dougwilson dougwilson added the pr label Nov 9, 2014
@emech-en
Copy link
Author

emech-en commented Nov 9, 2014

You'r right 👍

@dougwilson
Copy link
Contributor

So this was my stab in the dark at it, following what was in your PR, but without bothering with the debug stuff. Hopefully this helps you out :)

var carrier = require('carrier')
var mongodb = require('mongodb')
var morgan = require('morgan')
var stream = require('stream')

var MongoClient = mongodb.MongoClient
var PassThroughStream = stream.PassThrough

module.exports = mongoMorgan

function mongoMorgan(format, mongodbUrl, options) {
  options = options || {}

  var buffer = []
  var collection = options.collection || 'request'

  // create stream for morgan to write to
  var stream = new PassThroughStream()

  // create stream to read from
  var lineStream = carrier.carry(stream)
  lineStream.on('line', onLine)

  // create mongo client
  var mongoCollection = null
  MongoClient.connect(mongodbUrl, onConnect)

  // mixin options
  options.stream = stream

  function onConnect(error, mongoDb) {
    if (error) {
      throw error
    }

    mongoCollection = mongoDb.collection(collection)
  }

  function onLine(line) {
    var entry = {
      time: Date.now(),
      request: line
    }

    buffer.push(entry)

    if (!mongoCollection) {
      return
    }

    while (buffer.length !== 0) {
      entry = buffer.shift()
      mongoCollection.insert(entry)
    }
  }

  return morgan(format, options)
}

@emech-en
Copy link
Author

That's awesome :)
But isn't it bad to keep the db connection open during the program?
Thank you

@dougwilson
Copy link
Contributor

No, it's a server, you want to keep the connection open. Otherwise you are opening and closing a connection for every single request. Think you you had 500 req/s what it would do to your MongoDB.

@dougwilson dougwilson self-assigned this Jul 3, 2015
@expressjs expressjs locked and limited conversation to collaborators Jul 3, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants