fixed stuff; args. added: stuff.

This commit is contained in:
Strix 2022-05-07 16:07:00 +02:00
parent 9e38f0b3e3
commit ce0e6a1333
Signed by untrusted user who does not match committer: didier
GPG key ID: 01E71F18AA4398E5

View file

@ -5,17 +5,23 @@ class Logger {
*
* @param {{
* output: NodeJS.WritableStream,
* output2: NodeJS.WritableStream,
* template: string,
* argsPlaceholder: string,
* levelsColors: {[level: string]: string},
* historySize: number,
* defaultLevel: string
* onHistoryFull: () => void,
* argsPlaceholder: string,
* dict: {[key: string]: string}
* logFrom: string
* logFrom2: string
* }} options
*/
constructor(options = {})
{
this.output = options.output || process.stdout;
this.output2 = options.output2 || null;
this.template = options.template || "[{level} {timestamp}] {message}";
this.levelsColors = { // no same colors
this.levelsColors = options.levelsColors || { // no same colors
debug: "\x1b[32m",
info: "\x1b[36m",
warn: "\x1b[33m",
@ -32,6 +38,12 @@ class Logger {
this.alog("warn;", "History is full, dropping 25 oldest entries.");
this.history = this.history.slice(25);
};
this.logFrom = options.logFrom || "D"; // when to stop logging to output
this.logFrom2 = options.logFrom2 || "D"; // when to stop logging to output2
// Order from least important to most: D, I, W, E, F
this.logImportance = {
D: 0, I: 1, W: 2, E: 3, F: 4
};
}
/** log
@ -41,16 +53,16 @@ class Logger {
*
*/
log (message, ...args)
{this.alog(this.defaultLevel, message, args);}
{this.alog(this.defaultLevel, message, ...args);}
/** alog
*
* @param {string} level
* @param {'debug' | 'info' | 'warn' | 'error' | 'fatal'} level
* @param {string} message
* @param {any} ...args
*
*/
alog(level, message, args = [])
async alog(level, message, ...args)
{
//replace placeholders with args
let msg = this.template;
@ -63,56 +75,33 @@ class Logger {
// [I 1/1/1970 00:00:00] spul {} {}
//replace placeholders with args
let argPlaceholder = this.argsPlaceholder;
let matches = (msg.match(new RegExp(argPlaceholder, "g")) || []).length;
let argsFilled = 0;
// args.forEach((v, i, a) => {
// if (Buffer.isBuffer(v))
// v = conversion.properHex(v).str;
// if (typeof v === 'object')
// v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m";
if (args.length > 0)
{
let argPH = this.argsPlaceholder;
for (let arg of args)
{
// detect if arg is an object and convert it to string if it is
if (typeof arg === "object" || Array.isArray(arg) || arg.constructor === Object)
arg = JSON.stringify(arg, null, 2);
// msg = msg.replace(argPlaceholder, v);
// argsFilled++;
// console.log(`${argPlaceholder} -> ${v}`);
// });
// regexp replace
let regexp = new RegExp(argPlaceholder, "g");
msg = msg.replace(regexp, (match, offset, string) => {
if (argsFilled >= matches)
return match;
let v = args[argsFilled];
if (Buffer.isBuffer(v))
v = conversion.properHex(v).str;
if (typeof v === 'object')
v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m";
argsFilled++;
return v;
});
if (matches !== argsFilled && args.length >= matches)
console.log("warn;", "Not all placeholders were replaced.\n" +
"Placeholders: " + matches + "\n" +
"Args: " + args.length + "\n" +
"Args filled: " + argsFilled + "\n" +
"Message: " + msg);
if (matches < args.length)
msg += ` (${args.length - matches} args overflow.)`;
else if (matches > args.length)
msg += ` (${matches - args.length} args missing.)`;
msg = msg.replace(argPH, arg);
}
}
//replace dictionary keys
if (this.dict)
msg = dictionaryConversion(msg, this.dict);
msg = conversion.dictionaryConversion(msg, this.dict);
// add color
msg = this.levelsColors[level] + msg + "\x1b[0m";
let msgNoColors = msg;
if (this.levelsColors && this.levelsColors[level])
msg = this.levelsColors[level] + msg + "\x1b[0m";
//log
this.output.write(msg + "\n");
//log (if important enough)
if (this.logImportance[level.charAt(0).toUpperCase()] >= this.logImportance[this.logFrom])
this.output.write(msg + "\n");
if (this.output2 && this.logImportance[level.charAt(0).toUpperCase()] >= this.logImportance[this.logFrom2])
this.output2.write(msgNoColors + "\n");
//save to history
this.history.push({