logdna / logdna-browser

A frontend browser logging and exception capturing library for LogDNA
MIT License
19 stars 14 forks source link

Feature request: allow custom JSON replacer function #46

Open rfink opened 2 years ago

rfink commented 2 years ago

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.

TerryMooreII commented 2 years ago

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))
        }
      ]
    },
});
rfink commented 2 years ago

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

TerryMooreII commented 2 years ago

@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.