-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-logger.js
79 lines (65 loc) · 1.89 KB
/
mongo-logger.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
function MongoLogger(srcComponent) {
var component = srcComponent;
this.levels = {
FATAL: 0,
ERROR: 1,
WARN: 2,
INFO: 3,
DEBUG: 4
};
// default log level
var level = this.levels.INFO;
var log = function (level, msg) {
if(msg === null || msg === undefined) {
msg = "";
}
var header = (now() + " " + level).padEnd(27, " ");
print(header + "[" + component + "] : " + msg);
};
var now = function () {
var tzOffset = (new Date()).getTimezoneOffset() * 60000;
var dateNow = (new Date(Date.now() - tzOffset)).toISOString()
dateNow = dateNow.replace("T", " ").slice(0, -5);
return dateNow;
};
this.setLogLevel = function(newLevel) {
level = newLevel;
};
this.getLogLevel = function() {
for (var [key, value] of Object.entries(this.levels)) {
if(level === value) {
return key;
}
}
};
this.debug = function (msg) {
if(level >= this.levels.DEBUG) {
log("DEBUG", msg);
}
};
this.info = function (msg) {
if(level >= this.levels.INFO) {
log("INFO", msg);
}
};
this.warn = function (msg) {
if(level >= this.levels.WARN) {
log("WARN", msg);
}
};
this.error = function (msg) {
if(level >= this.levels.ERROR) {
log("ERROR", msg);
}
};
this.fatal = function (msg) {
if(level >= this.levels.FATAL) {
log("FATAL", msg);
}
};
// I love dynamically-typed languages :)
if(srcComponent === null || srcComponent === undefined || srcComponent.trim().length === 0) {
component = (new Error).fileName;
this.warn("Using default component name since no source component name has been set when creating the Logger.");
}
}