-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongodb-output.js
79 lines (66 loc) · 2.65 KB
/
mongodb-output.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
//
// Output plugin that stores logs in MongoDB database.
//
var assert = require('chai').assert;
var MongoClient = require('mongodb').MongoClient;
module.exports = function (conf) {
console.log('Loading to database: ' + conf.get('db'));
console.log('Logs to: ' + conf.get('logsCollection'));
console.log('Errors to: ' + conf.get('errorsCollection'));
return MongoClient.connect(conf.get('db'))
.then(client => {
var db = client.db('logs');
var logsCollection = db.collection(conf.get('logsCollection'));
var errorsCollection = db.collection(conf.get('errorsCollection'));
return {
//
// Emit an array of logs to the database.
//
emit: function (logs) {
if (logs && logs.length > 0) {
logsCollection
.insertMany(logs)
.then(function () {
//console.log("Added logs to database.");
})
.catch(function (err) {
console.error("!! " + (err && err.stack || err));
});
}
var errorLogs = logs.filter(function (log) {
return log.Level === 'Fatal' || log.Level === 'Error';
});
if (errorLogs.length > 0) {
errorsCollection.insertMany(errorLogs)
.then(function () {
//console.log("Added errors to database");
})
.catch(function (err) {
console.error("!! " + (err && err.stack || err));
});
}
/*
logs.forEach(function (log) {
console.log(log.Properties.UserName + " | " + log.RenderedMessage);
});
*/
},
//
// Retreive logs that have been stored.
//
retrieveLogs: function (startDate, endDate) {
assert.instanceOf(startDate, Date);
assert.instanceOf(endDate, Date);
return logsCollection.find({
Timestamp: {
$gte: startDate,
$lte: endDate
},
})
.toArray()
;
},
};
});
};