[mirotalksfu] - add logs json format, update dep
هذا الالتزام موجود في:
@@ -1,9 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const util = require('util');
|
||||
|
||||
const colors = require('colors');
|
||||
|
||||
const config = require('./config');
|
||||
|
||||
config.system?.console?.colors ? colors.enable() : colors.disable();
|
||||
@@ -12,6 +10,10 @@ const options = {
|
||||
depth: null,
|
||||
colors: config.system?.console?.colors || false,
|
||||
};
|
||||
|
||||
const LOGS_JSON = config.system?.console?.json;
|
||||
const LOGS_JSON_PRETTY = config.system?.console?.json_pretty;
|
||||
|
||||
module.exports = class Logger {
|
||||
constructor(appName = 'miroTalkSfu') {
|
||||
this.appName = colors.yellow(appName);
|
||||
@@ -25,42 +27,76 @@ module.exports = class Logger {
|
||||
};
|
||||
}
|
||||
|
||||
jsonLog(level, appName, msg, op, extra = {}) {
|
||||
const logObj = {
|
||||
timestamp: new Date().toISOString(),
|
||||
level,
|
||||
app: appName,
|
||||
message: msg,
|
||||
...extra,
|
||||
};
|
||||
if (op && typeof op === 'object' && Object.keys(op).length > 0) {
|
||||
logObj.data = op;
|
||||
}
|
||||
LOGS_JSON_PRETTY ? console.log(JSON.stringify(logObj, null, 2)) : console.log(JSON.stringify(logObj));
|
||||
}
|
||||
|
||||
debug(msg, op = '') {
|
||||
if (this.debugOn) {
|
||||
this.timeEnd = Date.now();
|
||||
this.timeElapsedMs = this.getFormatTime(Math.floor(this.timeEnd - this.timeStart));
|
||||
console.debug(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + msg,
|
||||
util.inspect(op, options),
|
||||
this.timeElapsedMs
|
||||
);
|
||||
if (LOGS_JSON) {
|
||||
this.jsonLog('debug', this.appName, msg, op, { elapsed: this.timeElapsedMs });
|
||||
} else {
|
||||
console.debug(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + msg,
|
||||
util.inspect(op, options),
|
||||
this.timeElapsedMs
|
||||
);
|
||||
}
|
||||
this.timeStart = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
log(msg, op = '') {
|
||||
console.log('[' + this.getDateTime() + '] [' + this.appName + '] ' + msg, util.inspect(op, options));
|
||||
if (LOGS_JSON) {
|
||||
this.jsonLog('log', this.appName, msg, op);
|
||||
} else {
|
||||
console.log('[' + this.getDateTime() + '] [' + this.appName + '] ' + msg, util.inspect(op, options));
|
||||
}
|
||||
}
|
||||
|
||||
info(msg, op = '') {
|
||||
console.info(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.green(msg),
|
||||
util.inspect(op, options)
|
||||
);
|
||||
if (LOGS_JSON) {
|
||||
this.jsonLog('info', this.appName, msg, op);
|
||||
} else {
|
||||
console.info(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.green(msg),
|
||||
util.inspect(op, options)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
warn(msg, op = '') {
|
||||
console.warn(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.yellow(msg),
|
||||
util.inspect(op, options)
|
||||
);
|
||||
if (LOGS_JSON) {
|
||||
this.jsonLog('warn', this.appName, msg, op);
|
||||
} else {
|
||||
console.warn(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.yellow(msg),
|
||||
util.inspect(op, options)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
error(msg, op = '') {
|
||||
console.error(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.red(msg),
|
||||
util.inspect(op, options)
|
||||
);
|
||||
if (LOGS_JSON) {
|
||||
this.jsonLog('error', this.appName, msg, op);
|
||||
} else {
|
||||
console.error(
|
||||
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.red(msg),
|
||||
util.inspect(op, options)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
getDateTime(color = true) {
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم