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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# morgan

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
[![Gratipay][gratipay-image]][gratipay-url]
# morgan with mongodb option

HTTP request logger middleware for node.js

> Named after [Dexter](http://en.wikipedia.org/wiki/Dexter_Morgan), a show you should not watch until completion.


**This repository is a modified copy of [morgan](https://github.com/expressjs/morgan) and this copy is not on npm yet, so you need to manually copy this module to your project location**

## API

```js
var morgan = require('morgan')
var morgan = require('./path/to/morgan/module')
```

### morgan(format, options)
Expand Down Expand Up @@ -48,6 +45,30 @@ morgan('combined', {

Output stream for writing log lines, defaults to `process.stdout`.

##### mongodb

Mongodb connection string which you want to save your logs to.

```js
// EXAMPLE: also save logs to localhost:27017/morgan database
morgan('combined', {
mongodb: "mongodb://localhost:27017/morgan"
})
```

##### collection

Name of the collection which you want to save your logs to, defaults to `request`

```js
// EXAMPLE: also save logs to localhost:27017/morgan database and the log_request collection
morgan('combined', {
mongodb: "mongodb://localhost:27017/morgan",
collection: "log_request"
})
```


#### Predefined Formats

There are various pre-defined formats provided:
Expand Down
42 changes: 42 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var auth = require('basic-auth')
var debug = require('debug')('morgan')
var deprecate = require('depd')('morgan')
var onFinished = require('on-finished')
var MongoClient = require('mongodb').MongoClient;

/**
* Array of CLF month names.
Expand Down Expand Up @@ -67,6 +68,10 @@ exports = module.exports = function morgan(format, options) {
// format function
var fmt = compile(exports[format] || format || exports.default)

// url of mongodb options
var mongodbUrl = options.mongodb;
var mongodbCollection = options.collection || "request";

// steam
var buffer = options.buffer
var stream = options.stream || process.stdout
Expand Down Expand Up @@ -122,8 +127,45 @@ exports = module.exports = function morgan(format, options) {
return
}

function logToMongodb (line)
{
if (!mongodbUrl) {
debug('no mongodb');
return;
}

// connect to mongodb
MongoClient.connect(mongodbUrl,function(error,db) {
// check if error occured
if (error) {
debug("mongodb error", error);
db.close();
return;
}
// get collection for the logs
var collection = db.collection(mongodbCollection);
// create log object
var requestlog = {
time: Date.now(),
request: line
};
// insert log object to collection
collection.insert(requestlog, function(error, result){
if (error) {
debug("mongodb write error", error);
} else {
debug("log saved to mongodb");
}

debug("mongodb closed");
db.close();
});
});
}

debug('log request')
stream.write(line + '\n')
logToMongodb(line)
};

// immediate
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"basic-auth": "1.0.0",
"debug": "~2.1.0",
"depd": "~1.0.0",
"on-finished": "2.1.1"
"on-finished": "2.1.1",
"mongodb": "~2.0"
},
"devDependencies": {
"istanbul": "0.3.2",
Expand Down
83 changes: 83 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var assert = require('assert');
var http = require('http');
var morgan = require('..');
var request = require('supertest');
var MongoClient = require('mongodb').MongoClient;

var lastLogLine;
function saveLastLogLine(line) { lastLogLine = line; }
Expand Down Expand Up @@ -679,6 +680,88 @@ describe('logger()', function () {
})
})
})

var mongodbUrl = 'mongodb://admin:123456@emech-Aspire-5738:27000/morgantest?authSource=admin';
describe('with mongodb option ' + mongodbUrl, function () {
it('should save log to default collection request', function (done) {
// removing all log containments
MongoClient.connect(mongodbUrl, function (error, db) {
// error occured in connecting to mongodb
if (error) return done('can not connect to mongodb: '+ error)
var collection = db.collection('request')
// removing the saved logs
collection.remove({}, function (error, result) {
// error in removing saved logs
if (error) return done('can not remove all logs saved to mongodb: '+ error)
// close db connection
db.close()

// create server with mongodb option
var server = createServer({'format': 'default', 'mongodb': mongodbUrl})
request(server)
.get('/')
.end(function (err, res) {
// connect to mongo for checking the log saved
MongoClient.connect(mongodbUrl, function (error, db) {
// cannot connect to mongo
if (error) return done('can not connect to mongodb: '+ error)

var collection = db.collection('request');
// counting the log count, there should be only one log
collection.count({}, function (error, result) {
// can not count the log collection
if (error) return done('can not count read count from mongodb: '+ error)

// check log count it should be 1
assert((result === 1), 'there should be 1 log but there is ' + result)
done()
})
})
})
})
})
})

var collectionName = 'log_request'
it('should save log to collection ' + collectionName, function (done) {
// removing all log containments
MongoClient.connect(mongodbUrl, function (error, db) {
// error occured in connecting to mongodb
if (error) return done('can not connect to mongodb: '+ error)
var collection = db.collection(collectionName)
// removing the saved logs
collection.remove({}, function (error, result) {
// error in removing saved logs
if (error) return done('can not remove all logs saved to mongodb: '+ error)
// close db connection
db.close()

// create server with mongodb option
var server = createServer({'format': 'default', 'mongodb': mongodbUrl, 'collection': collectionName})
request(server)
.get('/')
.end(function (err, res) {
// connect to mongo for checking the log saved
MongoClient.connect(mongodbUrl, function (error, db) {
// cannot connect to mongo
if (error) return done('can not connect to mongodb: '+ error)

var collection = db.collection(collectionName);
// counting the log count, there should be only one log
collection.count({}, function (error, result) {
// can not count the log collection
if (error) return done('can not count read count from mongodb: '+ error)

// check log count it should be 1
assert((result === 1), 'there should be 1 log but there is ' + result)
done()
})
})
})
})
})
})
})
})

function createLogger(format, opts) {
Expand Down