Skip to content

Commit

Permalink
use json-stringify-safe to support circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
objt-product committed May 7, 2021
1 parent a0b5bde commit 8e35b92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 82 deletions.
92 changes: 22 additions & 70 deletions log-elk-logger.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = function (RED) {
"use strict";
var debuglength = RED.settings.debugMaxLength || 1000;
var util = require("util");
const safeJSONStringify = require("json-stringify-safe");

function LogElkLoggerNode(config) {
var winston = require('winston');
Expand Down Expand Up @@ -114,66 +114,11 @@ module.exports = function (RED) {
return transformed;
};


function sendDebug(msg) {
if (msg.msg instanceof Error) {
msg.format = "error";
msg.msg = msg.msg.toString();
} else if (msg.msg instanceof Buffer) {
msg.format = "buffer [" + msg.msg.length + "]";
msg.msg = msg.msg.toString('hex');
} else if (msg.msg && typeof msg.msg === 'object') {
var seen = [];
try {
msg.format = msg.msg.constructor.name || "Object";
} catch (err) {
msg.format = "Object";
}
var isArray = util.isArray(msg.msg);
if (isArray) {
msg.format = "array [" + msg.msg.length + "]";
}
if (isArray || (msg.format === "Object")) {
msg.msg = JSON.stringify(msg.msg, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (seen.indexOf(value) !== -1) {
return "[circular]";
}
seen.push(value);
}
return value;
}, " ");
} else {
try {
msg.msg = msg.msg.toString();
}
catch (e) {
msg.msg = "[Type not printable]";
}
}
seen = null;
} else if (typeof msg.msg === "boolean") {
msg.format = "boolean";
msg.msg = msg.msg.toString();
} else if (typeof msg.msg === "number") {
msg.format = "number";
msg.msg = msg.msg.toString();
} else if (msg.msg === 0) {
msg.format = "number";
msg.msg = "0";
} else if (msg.msg === null || typeof msg.msg === "undefined") {
msg.format = (msg.msg === null) ? "null" : "undefined";
msg.msg = "(undefined)";
} else {
msg.format = "string [" + msg.msg.length + "]";
msg.msg = msg.msg;
}

if (msg.msg.length > debuglength) {
msg.msg = msg.msg.substr(0, debuglength) + " ....";
}
RED.comms.publish("debug", msg);
msg = RED.util.encodeObject(msg, {maxLength:debuglength});
RED.comms.publish("debug",msg);
}


RED.nodes.registerType("log-elk-logger", LogElkLoggerNode,
{
Expand All @@ -183,29 +128,36 @@ module.exports = function (RED) {
}
});

// get a value for a message path seperated with '.'
const get = (obj, path) =>
path
.replace(/\[([^\[\]]*)\]/g, '.$1.')
.split('.')
.filter(t => t !== '')
.reduce((prev, cur) => prev && prev[cur], obj);

LogElkLoggerNode.prototype.addToLog = function addTolog(loglevel, msg, complete) {
if (complete === true || complete === "complete" || complete === "true") {
// Log complete message
if (this.debugLog === true || this.debugLog === "true") {
sendDebug({id: this.id, name: this.name, topic: msg.topic, msg: msg, _path: msg._path});
}
if (this.logger) {
this.logger.log(loglevel, JSON.stringify(msg), msg.meta);
this.logger.log(loglevel, safeJSONStringify(msg), msg.meta);
}
}
else if (complete !== undefined && complete !== null && complete !== "" && complete !== false && complete !== "false") {
// Log part of message
var output;
try { output = RED.util.getMessageProperty(msg, complete); }
catch(err) {
node.error(err);
return;
}

if (this.debugLog === true || this.debugLog === "true") {
sendDebug({id: this.id, name: this.name, topic: msg.topic, msg: get(msg, complete), _path: msg._path});
sendDebug({id: this.id, name: this.name, topic: msg.topic, msg: output, _path: msg._path});
}
if (this.logger) {
this.logger.log(loglevel, JSON.stringify(get(msg, complete)), msg.meta);
if (typeof output === "string") {
this.logger.log(loglevel, output, msg.meta);
} else if (typeof output === "object") {
this.logger.log(loglevel, safeJSONStringify(output), msg.meta);
} else {
this.logger.log(loglevel, safeJSONStringify(output), msg.meta);
}
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions log-elk.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ module.exports = function (RED) {
var complete = config.complete;
var loglevel = config.loglevel || "debug";

// get a value for a message path seperated with '.'
const get = (obj, path) =>
path
.replace(/\[([^\[\]]*)\]/g, '.$1.')
.split('.')
.filter(t => t !== '')
.reduce((prev, cur) => prev && prev[cur], obj);


this.on('input', function(msg, send, done) {
if (node.logger)
{
Expand All @@ -28,7 +19,11 @@ module.exports = function (RED) {
level = loglevel;
} else {
// get loglevel from message
level = get(msg, loglevel);
try { level = RED.util.getMessageProperty(msg, loglevel); }
catch(err) {
level = "debug";
}

if (!(level === "error" || level === "warn" || level === "info" || level === "debug")) {
// invalid log level, default to debug
level = "debug";
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-log-elk",
"version": "1.2.0",
"version": "1.2.1",
"description": "A Node-RED logging node with multiple outputs using the winston and winston-elasticsearch logging libraries",
"main": "log-elk.js",
"scripts": {
Expand All @@ -27,6 +27,7 @@
"license": "MIT",
"dependencies": {
"winston": "^3.3.3",
"winston-elasticsearch": "^0.15.2"
"winston-elasticsearch": "^0.15.2",
"json-stringify-safe": "^5.0.1"
}
}

0 comments on commit 8e35b92

Please sign in to comment.