[call-me] - add logs json format
This commit is contained in:
@@ -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
@@ -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() {
|
||||
|
||||
Generated
+2
-2
@@ -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
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user