-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLogger.js
248 lines (211 loc) · 6.52 KB
/
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const EventEmitter = require("events").EventEmitter;
const fs = require("fs");
const os = require("os");
const Tools = require("./utils/Tools");
const util = require("util");
class Logger {
constructor() {
this._logEventEmitter = new EventEmitter();
this._logFileMaxSizeCheckLineCounter = 1;
this.logFileMaxSize = 4 * 1024 * 1024; //4MiB
this.logLevel = Logger.LogLevels["info"];
this.logFilePath = os.type() === "Windows_NT" ? Logger.DEFAULT_LOGFILE_PATHS.WINNT : Logger.DEFAULT_LOGFILE_PATHS.POSIX;
this.logFileWriteStream = fs.createWriteStream(this.logFilePath, Logger.LogFileOptions);
}
/**
* @public
* @return {string}
*/
getLogLevel() {
return Object.keys(Logger.LogLevels).find(key => {
return Logger.LogLevels[key] === this.logLevel;
});
}
/**
* @public
* @param {string} value
*/
setLogLevel(value) {
if (Logger.LogLevels[value] === undefined) {
throw new Error(`Invalid log level '${value}', valid are '${Object.keys(Logger.LogLevels).join("','")}'`);
} else {
this.logLevel = Logger.LogLevels[value];
}
}
/**
* @public
* @return {string}
*/
getLogFilePath() {
return this.logFilePath;
}
/**
* @public
* @param {string} filePath
*/
setLogFilePath(filePath) {
if (Tools.ARE_SAME_FILES(this.logFilePath, filePath)) {
// We are already writing to that file
return;
}
if (this.logFileWriteStream) {
this.logFileWriteStream.close();
this.logFileWriteStream = null;
}
this.logFilePath = filePath;
// Check if output is already redirected to that same file. If
// it is, we do not need to write to that same file, because that
// would lead to duplicate log entries.
// Setting the LogFilename anyway ensures that the UI Log still works.
if (!Tools.ARE_SAME_FILES(filePath, "/proc/self/fd/1")) {
this.logFileWriteStream = fs.createWriteStream(this.logFilePath, Logger.LogFileOptions);
}
this.log("info", "Set Logfile to " + filePath);
}
/**
* @private
* @param {string} logLevel
* @return {string}
*/
buildLogLinePrefix(logLevel) {
return `[${new Date().toISOString()}] [${logLevel}]`;
}
/**
* @param {string} level
* @param {...any} args
* @private
*/
log(level, ...args) {
if (this.logLevel["level"] <= Logger.LogLevels[level]["level"]) {
const logLinePrefix = this.buildLogLinePrefix(level.toUpperCase());
const logLine = [logLinePrefix, ...args].map(arg => {
if (typeof arg === "string") {
return arg;
}
return util.inspect(
arg,
{
depth: Infinity
}
);
}).join(" ");
Logger.LogLevels[level]["callback"](logLine);
this.logLineToFile(logLine);
this._logEventEmitter.emit("LogMessage", logLine);
}
}
/**
* @private
* @param {string} line
*/
logLineToFile(line) {
if (this.logFileWriteStream) {
/*
As the default limit is rather large, we can avoid checking the logfile size on every single
log line without running into any OOM issues
*/
this._logFileMaxSizeCheckLineCounter = (this._logFileMaxSizeCheckLineCounter + 1) % 100;
if (this._logFileMaxSizeCheckLineCounter === 0) {
if (
this.logFilePath !== Logger.DEFAULT_LOGFILE_PATHS.WINNT &&
this.logFilePath !== Logger.DEFAULT_LOGFILE_PATHS.POSIX
) {
let fileSize = 0;
try {
const stat = fs.statSync(this.logFilePath);
fileSize = stat.size;
} catch (e) {
this.error("Error while checking Logfile size:", e);
}
if (fileSize > this.logFileMaxSize) {
this.logFileWriteStream.close();
fs.writeFileSync(this.logFilePath, "");
this.logFileWriteStream = fs.createWriteStream(this.logFilePath, Logger.LogFileOptions);
this.warn(`Logfile ${this.logFilePath} was cleared after reaching a size of ${fileSize} bytes.`);
}
}
}
this.logFileWriteStream.write(line);
this.logFileWriteStream.write("\n");
}
}
/**
* @public
* @param {any} listener
*/
onLogMessage(listener) {
this._logEventEmitter.on("LogMessage", listener);
}
/**
* @public
* @see console.trace
* @param {...any} args
*/
trace(...args) {
this.log("trace", ...args);
}
/**
* @public
* @see console.debug
* @param {...any} args
*/
debug(...args) {
this.log("debug", ...args);
}
/**
* @public
* @see console.info
* @param {...any} args
*/
info(...args) {
this.log("info", ...args);
}
/**
* @public
* @see console.warn
* @param {...any} args
*/
warn(...args) {
this.log("warn", ...args);
}
/**
* @public
* @see console.error
* @param {...any} args
*/
error( ...args) {
this.log("error", ...args);
}
/**
* @public
*/
getProperties() {
return {
EVENTS: Logger.EVENTS,
LogLevels: Logger.LogLevels
};
}
}
Logger.EVENTS = {
LogMessage: "LogMessage",
};
Logger.LogLevels = Object.freeze({
// eslint-disable-next-line no-console
"trace": {"level": -2, "callback": console.debug},
// eslint-disable-next-line no-console
"debug": {"level": -1, "callback": console.debug},
// eslint-disable-next-line no-console
"info": {"level": 0, "callback": console.info},
// eslint-disable-next-line no-console
"warn": {"level": 1, "callback": console.warn},
// eslint-disable-next-line no-console
"error": {"level": 2, "callback": console.error},
});
Logger.LogFileOptions = Object.freeze({
flags: "as"
});
Logger.DEFAULT_LOGFILE_PATHS = Object.freeze({
POSIX: "/dev/null",
WINNT: "\\\\.\\NUL"
});
module.exports = new Logger();