idkanymore
This commit is contained in:
parent
2ec332dc68
commit
de3e95c383
2 changed files with 29 additions and 18 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
node_modules
|
|
@ -19,6 +19,7 @@ class Logger {
|
|||
debug: "\x1b[32m",
|
||||
info: "\x1b[36m",
|
||||
warn: "\x1b[33m",
|
||||
"warn;": "\x1b[33m",
|
||||
error: "\x1b[31m",
|
||||
fatal: "\x1b[35m"
|
||||
};
|
||||
|
@ -28,7 +29,8 @@ class Logger {
|
|||
this.history = [];
|
||||
this.historySize = options.historySize || 100;
|
||||
this.onHistoryFull = options.onHistoryFull || function() {
|
||||
// TODO: implement
|
||||
this.alog("warn;", "History is full, dropping 25 oldest entries.");
|
||||
this.history = this.history.slice(25);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -39,9 +41,7 @@ class Logger {
|
|||
*
|
||||
*/
|
||||
log (message, ...args)
|
||||
{
|
||||
this.alog(this.defaultLevel, message, ...args);
|
||||
}
|
||||
{this.alog(this.defaultLevel, message, args);}
|
||||
|
||||
/** alog
|
||||
*
|
||||
|
@ -50,35 +50,45 @@ class Logger {
|
|||
* @param {any} ...args
|
||||
*
|
||||
*/
|
||||
alog(level, message, ...args)
|
||||
alog(level, message, args = [])
|
||||
{
|
||||
//replace placeholders with args
|
||||
let msg = this.template;
|
||||
|
||||
msg = msg.replace(/\{timestamp\}/g, new Date().toISOString());
|
||||
msg = msg.replace(/\{level\}/g, level.charAt(0).toUpperCase());
|
||||
// [I {timestamp}] {message}
|
||||
msg = msg.replace(/\{timestamp\}/g, new Date().toISOString());
|
||||
// [I 1/1/1970 00:00:00] {message}
|
||||
msg = msg.replace(/\{message\}/g, message);
|
||||
// [I 1/1/1970 00:00:00] spul {} {}
|
||||
|
||||
//replace placeholders with args
|
||||
let argPlaceholder = this.argsPlaceholder;
|
||||
let regex = new RegExp(argPlaceholder, "g");
|
||||
let stringArgCount = (regex.exec(msg) || []).length;
|
||||
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 = JSON.stringify(v);
|
||||
}
|
||||
v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m";
|
||||
|
||||
msg = msg.replace(argPlaceholder, v);
|
||||
argsFilled++;
|
||||
console.log(`${argPlaceholder} -> ${v}`);
|
||||
});
|
||||
if (stringArgCount < args.length)
|
||||
msg += ` (${args.length - stringArgCount} args overflow.)`;
|
||||
else if (stringArgCount > args.length)
|
||||
msg += ` (${stringArgCount - args.length} args missing.)`;
|
||||
|
||||
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.)`;
|
||||
|
||||
//replace dictionary keys
|
||||
if (this.dict)
|
||||
|
@ -96,7 +106,7 @@ class Logger {
|
|||
message: message,
|
||||
args: args
|
||||
});
|
||||
if (this.history.length >= this.historySize)
|
||||
if (this.history.length >= this.historySize && level !== "warn;")
|
||||
this.onHistoryFull();
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue