fixed stuff; args. added: stuff.
This commit is contained in:
parent
9e38f0b3e3
commit
ce0e6a1333
1 changed files with 41 additions and 52 deletions
|
@ -4,18 +4,24 @@ class Logger {
|
||||||
/** Logger
|
/** Logger
|
||||||
*
|
*
|
||||||
* @param {{
|
* @param {{
|
||||||
* output: NodeJS.WritableStream,
|
* output: NodeJS.WritableStream,
|
||||||
* template: string,
|
* output2: NodeJS.WritableStream,
|
||||||
* argsPlaceholder: string,
|
* template: string,
|
||||||
* historySize: number,
|
* levelsColors: {[level: string]: string},
|
||||||
* defaultLevel: string
|
* historySize: number,
|
||||||
|
* onHistoryFull: () => void,
|
||||||
|
* argsPlaceholder: string,
|
||||||
|
* dict: {[key: string]: string}
|
||||||
|
* logFrom: string
|
||||||
|
* logFrom2: string
|
||||||
* }} options
|
* }} options
|
||||||
*/
|
*/
|
||||||
constructor(options = {})
|
constructor(options = {})
|
||||||
{
|
{
|
||||||
this.output = options.output || process.stdout;
|
this.output = options.output || process.stdout;
|
||||||
|
this.output2 = options.output2 || null;
|
||||||
this.template = options.template || "[{level} {timestamp}] {message}";
|
this.template = options.template || "[{level} {timestamp}] {message}";
|
||||||
this.levelsColors = { // no same colors
|
this.levelsColors = options.levelsColors || { // no same colors
|
||||||
debug: "\x1b[32m",
|
debug: "\x1b[32m",
|
||||||
info: "\x1b[36m",
|
info: "\x1b[36m",
|
||||||
warn: "\x1b[33m",
|
warn: "\x1b[33m",
|
||||||
|
@ -32,6 +38,12 @@ class Logger {
|
||||||
this.alog("warn;", "History is full, dropping 25 oldest entries.");
|
this.alog("warn;", "History is full, dropping 25 oldest entries.");
|
||||||
this.history = this.history.slice(25);
|
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
|
/** log
|
||||||
|
@ -41,16 +53,16 @@ class Logger {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
log (message, ...args)
|
log (message, ...args)
|
||||||
{this.alog(this.defaultLevel, message, args);}
|
{this.alog(this.defaultLevel, message, ...args);}
|
||||||
|
|
||||||
/** alog
|
/** alog
|
||||||
*
|
*
|
||||||
* @param {string} level
|
* @param {'debug' | 'info' | 'warn' | 'error' | 'fatal'} level
|
||||||
* @param {string} message
|
* @param {string} message
|
||||||
* @param {any} ...args
|
* @param {any} ...args
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
alog(level, message, args = [])
|
async alog(level, message, ...args)
|
||||||
{
|
{
|
||||||
//replace placeholders with args
|
//replace placeholders with args
|
||||||
let msg = this.template;
|
let msg = this.template;
|
||||||
|
@ -63,56 +75,33 @@ class Logger {
|
||||||
// [I 1/1/1970 00:00:00] spul {} {}
|
// [I 1/1/1970 00:00:00] spul {} {}
|
||||||
|
|
||||||
//replace placeholders with args
|
//replace placeholders with args
|
||||||
let argPlaceholder = this.argsPlaceholder;
|
if (args.length > 0)
|
||||||
let matches = (msg.match(new RegExp(argPlaceholder, "g")) || []).length;
|
{
|
||||||
let argsFilled = 0;
|
let argPH = this.argsPlaceholder;
|
||||||
// args.forEach((v, i, a) => {
|
for (let arg of args)
|
||||||
// if (Buffer.isBuffer(v))
|
{
|
||||||
// v = conversion.properHex(v).str;
|
// detect if arg is an object and convert it to string if it is
|
||||||
// if (typeof v === 'object')
|
if (typeof arg === "object" || Array.isArray(arg) || arg.constructor === Object)
|
||||||
// v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m";
|
arg = JSON.stringify(arg, null, 2);
|
||||||
|
|
||||||
// msg = msg.replace(argPlaceholder, v);
|
msg = msg.replace(argPH, arg);
|
||||||
// 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.)`;
|
|
||||||
|
|
||||||
//replace dictionary keys
|
//replace dictionary keys
|
||||||
if (this.dict)
|
if (this.dict)
|
||||||
msg = dictionaryConversion(msg, this.dict);
|
msg = conversion.dictionaryConversion(msg, this.dict);
|
||||||
|
|
||||||
// add color
|
// 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
|
//log (if important enough)
|
||||||
this.output.write(msg + "\n");
|
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
|
//save to history
|
||||||
this.history.push({
|
this.history.push({
|
||||||
|
|
Reference in a new issue