From e6af5839e0386e41c58715dc95e9584c9db03072 Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Sun, 13 Jul 2025 19:21:52 +0200 Subject: [PATCH] [call-me] - add logs json format --- .env.template | 5 ++++ app/logs.js | 74 ++++++++++++++++++++++++++++++++++------------- package-lock.json | 4 +-- package.json | 2 +- 4 files changed, 62 insertions(+), 23 deletions(-) diff --git a/.env.template b/.env.template index 230b4dc..3166b80 100644 --- a/.env.template +++ b/.env.template @@ -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 diff --git a/app/logs.js b/app/logs.js index e65f86d..467bce0 100644 --- a/app/logs.js +++ b/app/logs.js @@ -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() { diff --git a/package-lock.json b/package-lock.json index 40c5fcd..e89a701 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index ed488cb..3b73bc7 100755 --- a/package.json +++ b/package.json @@ -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",