logdna-browser icon indicating copy to clipboard operation
logdna-browser copied to clipboard

Feature request: allow custom JSON replacer function

Open rfink opened this issue 3 years ago • 3 comments

Allow custom JSON replacer function to be passed in via options, which will allow us to sanitize sensitive data from the logs (passwords, cc info, etc), helpful if objects passed into meta. Pull request incoming.

rfink avatar Jul 15 '22 14:07 rfink

This should be able to be accomplished by adding a custom beforeSend hook.

This will parse both the message and the lineContext

const jsonReplacer = (key, value) => {
  if (key === 'password') {
    return '[redacted]';
  }
  return value;
};

logdna.init('ingestionkey', {
  hooks: {
      beforeSend: [
        (line) => {
          return JSON.parse(JSON.stringify(line, jsonReplacer))
        }
      ]
    },
});

TerryMooreII avatar Jul 15 '22 15:07 TerryMooreII

@TerryMooreII thanks. Wouldn't this defeat the purpose of using the fast serializer?

rfink avatar Jul 15 '22 15:07 rfink

@rfink True, you could require/import fast-safe-stringify or any other stringify package and use it in your hook. We mostly use fast-safe-stringify because of its circular reference detection.

TerryMooreII avatar Jul 15 '22 15:07 TerryMooreII