-
Notifications
You must be signed in to change notification settings - Fork 0
/
logjammer.js
138 lines (109 loc) · 3.47 KB
/
logjammer.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function defaultData() {
return {};
}
const LogJammer = {
Severity: {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
FATAL: 4,
UNKNOWN: 5
}
};
Object.freeze(LogJammer.Severity);
LogJammer.severityOf = (severityStr) => {
return severityStr ? LogJammer.Severity[severityStr.toUpperCase()] : null;
};
LogJammer.config = {
application: '',
logLevel: process.env.LOG_LEVEL ? LogJammer.severityOf(process.env.LOG_LEVEL) : LogJammer.Severity.INFO,
origin: {},
globalData: {},
pretty: false,
device: console,
publisher: {
publish: (logEvent, pretty=false) => {
var json;
if (pretty) {
json = JSON.stringify(logEvent, null, 2);
} else {
json = JSON.stringify(logEvent);
}
LogJammer.config.device.log(json);
}
}
};
LogJammer.logger = (name) => {
var thisLogger = {
name: name || LogJammer.config.name,
logLevel: LogJammer.config.logLevel,
origin: LogJammer.config.origin,
globalData: LogJammer.config.globalData,
pretty: LogJammer.config.pretty,
publisher: LogJammer.config.publisher,
__buildEvent: (severity, message, data, err) => {
if (!LogJammer.config.application) {
throw "LogJammer.config.application must be set!";
}
var logEvent = {
severity: severity,
logger: thisLogger.name,
message: message,
origin: LogJammer.config.origin
};
if (err) {
var backtrace = err.stack.split('\n');
backtrace.shift();
logEvent.error = {
type: err.name,
code: err.code,
details: err.message,
backtrace: backtrace
};
}
data = data ? data : defaultData;
logEvent[LogJammer.config.application] = Object.assign(data(), LogJammer.config.globalData);
return logEvent;
}
};
thisLogger.debug = (msg, data=defaultData, err) => {
if (thisLogger.logLevel <= LogJammer.Severity.DEBUG) {
var logEvent = thisLogger.__buildEvent('debug', msg, data, err);
thisLogger.publisher.publish(logEvent, thisLogger.pretty);
}
};
thisLogger.info = (msg, data=defaultData, err) => {
if (thisLogger.logLevel <= LogJammer.Severity.INFO) {
var logEvent = thisLogger.__buildEvent('info', msg, data, err);
thisLogger.publisher.publish(logEvent, thisLogger.pretty);
}
};
thisLogger.warn = (msg, data=defaultData, err) => {
if (thisLogger.logLevel <= LogJammer.Severity.WARN) {
var logEvent = thisLogger.__buildEvent('warn', msg, data, err);
thisLogger.publisher.publish(logEvent, thisLogger.pretty);
}
};
thisLogger.error = (msg, data=defaultData, err) => {
if (thisLogger.logLevel <= LogJammer.Severity.ERROR) {
var logEvent = thisLogger.__buildEvent('error', msg, data, err);
thisLogger.publisher.publish(logEvent, thisLogger.pretty);
}
};
thisLogger.fatal = (msg, data=defaultData, err) => {
if (thisLogger.logLevel <= LogJammer.Severity.FATAL) {
var logEvent = thisLogger.__buildEvent('fatal', msg, data, err);
thisLogger.publisher.publish(logEvent, thisLogger.pretty);
}
};
thisLogger.unknown = (msg, data=defaultData, err) => {
if (thisLogger.logLevel <= LogJammer.Severity.UNKNOWN) {
var logEvent = thisLogger.__buildEvent('unknown', msg, data, err);
thisLogger.publisher.publish(logEvent, thisLogger.pretty);
}
};
thisLogger.origin.application = LogJammer.config.application;
return thisLogger;
};
module.exports = LogJammer;