-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
loggermongo.js
executable file
·118 lines (101 loc) · 2.72 KB
/
loggermongo.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import { Logger } from 'meteor/ostrio:logger';
import { check, Match } from 'meteor/check';
const noop = () => {};
const helpers = {
isObject(obj) {
if (this.isArray(obj) || this.isFunction(obj)) {
return false;
}
return obj === Object(obj);
},
isArray(obj) {
return Array.isArray(obj);
},
isFunction(obj) {
return typeof obj === 'function';
},
isString(obj) {
return Object.prototype.toString.call(obj) === '[object String]';
}
};
/**
* @class LoggerMongo
* @summary MongoDB adapter for ostrio:logger (Logger)
*/
class LoggerMongo {
constructor(logger, options = {}) {
check(logger, Match.OneOf(Logger, Object));
check(options, {
collectionName: Match.Optional(String),
collection: Match.Optional(Match.OneOf(Mongo.Collection, Object)),
format: Match.Optional(Function)
});
this.logger = logger;
if (!helpers.isFunction(options.format)) {
options.format = (opts) => {
return opts;
};
}
this.options = options;
if (Meteor.isServer) {
if (this.options.collection) {
this.collection = this.options.collection;
} else {
if (!this.options.collectionName) {
this.options.collectionName = 'ostrioMongoLogger';
}
this.collection = new Meteor.Collection(this.options.collectionName);
this.collection.deny({
update() {
return true;
},
remove() {
return true;
},
insert() {
return true;
}
});
}
}
this.logger.add('Mongo', (level, message, data, userId) => {
if (Meteor.isServer) {
const time = new Date();
const record = options.format({
userId: userId,
date: time,
timestamp: +time,
level: level,
message: message,
additional: data
});
if (!helpers.isObject(record)) {
throw new Meteor.Error(400, '[ostrio:logger] [options.format]: Must return a plain Object!', record);
}
this.collection.insert(record, noop);
}
}, noop, false, false);
}
enable(rule = {}) {
check(rule, {
enable: Match.Optional(Boolean),
client: Match.Optional(Boolean),
server: Match.Optional(Boolean),
filter: Match.Optional([String])
});
if (typeof rule.enable === 'undefined') {
rule.enable = true;
}
if (typeof rule.client === 'undefined') {
rule.client = true;
}
if (typeof rule.server === 'undefined') {
rule.server = true;
}
this.logger.rule('Mongo', rule);
return this;
}
}
export { LoggerMongo };