Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

mongodb username and password support

  • Loading branch information...
commit 58af741cdad970a0007ca9cb03c9db1cb016c9eb 1 parent 0b457f2
@rjrodger authored
Showing with 34 additions and 15 deletions.
  1. +34 −15 lib/winston/transports/mongodb.js
View
49 lib/winston/transports/mongodb.js
@@ -30,6 +30,8 @@ var MongoDB = exports.MongoDB = function (options) {
this.safe = options.safe || true;
this.level = options.level || 'info';
this.silent = options.silent || false;
+ this.username = options.username || null;
+ this.password = options.password || null;
};
//
@@ -47,30 +49,47 @@ MongoDB.prototype.log = function (level, msg, meta, callback) {
native_parser : false
});
+
client.open(function (err, db) {
if (err) {
return callback(err, false);
}
-
- db.collection(self.collection, function (err, col) {
- if (err) {
- return callback(err, false);
- }
- var entry = {
- level: level,
- message: msg,
- meta: meta
- };
-
- col.save(entry, { safe: self.safe }, function (err, doc) {
+ if( self.username && self.password ) {
+ client.authenticate( self.username, self.password, function(err){
if (err) {
return callback(err, false);
}
+
+ collection()
+ })
+ }
+ else {
+ collection()
+ }
- db.close();
- callback(null, true);
+ function collection() {
+ db.collection(self.collection, function (err, col) {
+ if (err) {
+ return callback(err, false);
+ }
+
+ var entry = {
+ level: level,
+ message: msg,
+ meta: meta
+ };
+
+ col.save(entry, { safe: self.safe }, function (err, doc) {
+ if (err) {
+ return callback(err, false);
+ }
+
+ db.close();
+ callback(null, true);
+ });
});
- });
+ }
+
});
};
Please sign in to comment.
Something went wrong with that request. Please try again.