[call-me] - add logs json format

This commit is contained in:
Miroslav Pejic
2025-07-13 19:21:52 +02:00
parent 2f80373a08
commit e6af5839e0
4 changed files with 62 additions and 23 deletions
+5
View File
@@ -7,6 +7,11 @@ NODE_ENV=development #development or production
HOST='' # Default http://localhost:port
PORT=8000
# Logging
LOGS_JSON=false # Enable JSON formatted logs (true/false)
LOGS_JSON_PRETTY=false # Enable pretty JSON formatted logs (true/false)
# Host
HOST_PASSWORD_ENABLED=false # true or false
+54 -20
View File
@@ -1,11 +1,12 @@
'use strict';
const util = require('util');
const colors = require('colors');
colors.enable(); // colors.disable();
const LOGS_JSON = process.env.LOGS_JSON ? process.env.LOGS_JSON === 'true' : false;
const LOGS_JSON_PRETTY = process.env.LOGS_JSON_PRETTY ? process.env.LOGS_JSON_PRETTY === 'true' : false;
const options = {
depth: null,
colors: true,
@@ -28,38 +29,71 @@ module.exports = class Logs {
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.info(
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.yellow(msg),
util.inspect(op, options)
);
if (LOGS_JSON) {
this.jsonLog('warn', this.appName, msg, op);
} else {
console.info(
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.yellow(msg),
util.inspect(op, options)
);
}
}
error(msg, op = '') {
console.info(
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.red(msg),
util.inspect(op, options)
);
if (LOGS_JSON) {
this.jsonLog('error', this.appName, msg, op);
} else {
console.info(
'[' + this.getDateTime() + '] [' + this.appName + '] ' + colors.red(msg),
util.inspect(op, options)
);
}
}
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));
}
getDateTime() {
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "call-me",
"version": "1.0.90",
"version": "1.0.91",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "call-me",
"version": "1.0.90",
"version": "1.0.91",
"license": "AGPLv3",
"dependencies": {
"@ngrok/ngrok": "1.5.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "call-me",
"version": "1.0.90",
"version": "1.0.91",
"description": "Your Go-To for Instant Video Calls",
"author": "Miroslav Pejic - miroslav.pejic.85@gmail.com",
"license": "AGPLv3",