-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.js
226 lines (178 loc) · 6.4 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
const fs = require("fs");
const path = require("path");
const stream = require("stream");
const assert = require("./utilities/type-utilities.js");
const { LOGS_DIR_PATH } = require("./constants.js");
const LEAN_LEVEL = 0;
const NORMAL_LEVEL = 1;
const VERBOSE_LEVEL = 2;
const WRITE_ONLY_LEVEL = 99;
var currentLogLevel = +process.env.LOG_LEVEL;
var isLoggingToConsole = process.env.LOG_TO_CONSOLE ?? true;
var isLoggingToFile = true;
var dayOfMonth = new Date().getDate();
var backupWriteStream;
var cleanerWriteStream;
var generalWriteStream;
var errorWriteStream;
var uploadWriteStream;
if (fs.existsSync(LOGS_DIR_PATH) === false)
fs.mkdirSync(LOGS_DIR_PATH);
_updateStreamPaths();
module.exports.getLeanLevel = () => LEAN_LEVEL;
module.exports.getNormalLevel = () => NORMAL_LEVEL;
module.exports.getVerboseLevel = () => VERBOSE_LEVEL;
module.exports.getWriteOnlyLevel = () => WRITE_ONLY_LEVEL;
module.exports.setLogLevel = (level) =>
{
if (assert.isInteger(level) === false)
return;
logLevel = level;
exports.general(LEAN_LEVEL, `logLevel set to ${level}.`);
};
module.exports.isLoggingToFile = () => isLoggingToFile;
module.exports.setLogToFile = (shouldLogToFile) =>
{
if (assert.isBoolean(shouldLogToFile) === false)
return;
isLoggingToFile = shouldLogToFile;
exports.general(LEAN_LEVEL, `isLoggingToFile set to ${shouldLogToFile}.`);
};
// Used by the backup_script.js, does not need to print to console
// as it's a separate console window. If it does, it will be picked
// by the stdio pipes and emitted to the master server
module.exports.backup = (logLevel, header, ...data) =>
{
var logStr = _formatEntry(header, ...data);
_log(logLevel, logStr);
_logToFile(logStr, backupWriteStream);
};
module.exports.cleaner = (logLevel, header, ...data) =>
{
var logStr = _formatEntry(header, ...data);
_log(logLevel, logStr);
_logToFile(logStr, cleanerWriteStream);
};
module.exports.general = (logLevel, header, ...data) =>
{
var logStr = _formatEntry(header, ...data);
_log(logLevel, logStr);
_logToFile(logStr, generalWriteStream);
};
module.exports.error = (logLevel, header, ...data) =>
{
var logStr = _formatEntry(header, ...data);
_log(logLevel, logStr);
_logToFile(logStr, errorWriteStream);
};
module.exports.upload = (logLevel, header, ...data) =>
{
var logStr = _formatEntry(header, ...data);
_log(logLevel, logStr);
_logToFile(logStr, uploadWriteStream);
};
module.exports.toFile = (filePath, header, ...data) =>
{
var logStr = _formatEntry(header, ...data);
var writeStream = fs.createWriteStream(filePath, { flags: "a", autoClose: true });
_logToFile(logStr, writeStream);
writeStream.end();
};
const timers = {};
module.exports.time = (tag, display) =>
{
timers[tag] = {
start: Date.now(),
display: display ?? tag
};
};
module.exports.timeEnd = (tag, logLevel) =>
{
logLevel = logLevel ?? NORMAL_LEVEL;
const data = timers[tag];
delete timers[tag];
if (data == null)
return;
const timeTaken = (Date.now() - data.start) * 0.001;
module.exports.general(logLevel, `${data.display}: ${timeTaken.toFixed(3)}s`);
};
function _formatEntry(header, ...data)
{
var logStr = `${_getTimestamp()}\t${header}\n`;
data.forEach((line) =>
{
if (assert.isObject(line) === true)
logStr += "\n" + _indentJSON(line);
else logStr += `\n\t${line}`;
});
logStr += "\n";
return logStr;
}
function _log(logLevel, logStr)
{
if (logLevel <= currentLogLevel && isLoggingToConsole === true)
console.log(logStr);
}
function _logToFile(logStr, writeStream)
{
if (isLoggingToFile === false)
return;
_updateStreamPaths();
writeStream.write(logStr);
}
function _updateStreamPaths()
{
const date = new Date();
const day = date.getDate();
if (dayOfMonth === day &&
assert.isInstanceOfPrototype(backupWriteStream, stream.Writable) === true &&
assert.isInstanceOfPrototype(cleanerWriteStream, stream.Writable) === true &&
assert.isInstanceOfPrototype(generalWriteStream, stream.Writable) === true &&
assert.isInstanceOfPrototype(errorWriteStream, stream.Writable) === true &&
assert.isInstanceOfPrototype(uploadWriteStream, stream.Writable) === true)
return;
dayOfMonth = day;
if (backupWriteStream != null && backupWriteStream.destroyed === false)
backupWriteStream.destroy();
if (cleanerWriteStream != null && cleanerWriteStream.destroyed === false)
cleanerWriteStream.destroy();
if (generalWriteStream != null && generalWriteStream.destroyed === false)
generalWriteStream.destroy();
if (errorWriteStream != null && errorWriteStream.destroyed === false)
errorWriteStream.destroy();
if (uploadWriteStream != null && uploadWriteStream.destroyed === false)
uploadWriteStream.destroy();
backupWriteStream = fs.createWriteStream(_getLogPath(date, "backup.txt"), { flags: "a", autoClose: true });
cleanerWriteStream = fs.createWriteStream(_getLogPath(date, "backup.txt"), { flags: "a", autoClose: true });
generalWriteStream = fs.createWriteStream(_getLogPath(date, "general.txt"), { flags: "a", autoClose: true });
errorWriteStream = fs.createWriteStream(_getLogPath(date, "error.txt"), { flags: "a", autoClose: true });
uploadWriteStream = fs.createWriteStream(_getLogPath(date, "upload.txt"), { flags: "a", autoClose: true });
}
function _getLogPath(date, filename)
{
return path.resolve(LOGS_DIR_PATH, `${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}-${filename}`);
}
function _getTimestamp()
{
return new Date(Date.now()).toISOString().replace(/^(.+)(T)/i, "$1$2 ");
}
// Stringify a json object with full indentation
function _indentJSON(obj)
{
// Replace the normal stringification if the object is an Error,
// otherwise they will show as empty {} objects
const jsonStr = JSON.stringify(obj, function replacer(objKey, objValue)
{
const err = {};
if (objValue instanceof Error)
{
Object.getOwnPropertyNames(objValue).forEach((key) => err[key] = objValue[key]);
return err;
}
return objValue;
}, 4);
// Split the resulting JSON string by newlines and/or escaped newlines
const split = jsonStr.split(/\n|\\n/g);
// Rejoin them with added indentation
return "\t" + split.join("\n\t") + "\n";
}