Skip to content

Commit

Permalink
redact log output
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyc committed Dec 13, 2024
1 parent 9ad0f66 commit 1938006
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions util/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const config = winston.config;
const loggerManager = require('./log_mgr.js');
const path = require('path');
const fs = require('fs');
const _ = require('lodash');

const moment = require('moment')

Expand All @@ -40,13 +41,33 @@ if (process.env.FWDEBUG) {
console.log("LOGGER SET TO", globalLogLevel);
}

const keysToRedact = new Set(["password", "passwd", "psk", "key", "psks"]);
// pass in function arguments object and returns string with whitespaces
function redactLog(obj, redactRequired = false) {
if (!obj)
return obj;
// obj should be either object or array
try {
for (const key of Object.keys(obj)) {
if (_.isObject(obj[key]) || _.isArray(obj[key]))
redactLog(obj[key], redactRequired || keysToRedact.has(key));
else {
if (redactRequired || keysToRedact.has(key))
obj[key] = "*** redacted ***";
}
}
} catch (err) {}
}

// pass in function arguments object and returns string with whitespaces
function argumentsToString(v) {
// convert arguments object to real array
var args = Array.prototype.slice.call(v);
for (var k in args) {
if (typeof args[k] === "object") {
// args[k] = JSON.stringify(args[k]);
args[k] = JSON.parse(JSON.stringify(args[k]));
redactLog(args[k]);
args[k] = require('util').inspect(args[k], false, null, true);
}
}
Expand Down

0 comments on commit 1938006

Please sign in to comment.