X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=src%2Flogger.js;h=2a205042f9b017df28980f206698e39817dca9fe;hb=HEAD;hp=00edfb8bcc0ceae419bd5e54a47bc0bb9b8684c9;hpb=3b791da2fe22568f567d4796895f223cdf212b9a;p=websub-hub diff --git a/src/logger.js b/src/logger.js deleted file mode 100644 index 00edfb8..0000000 --- a/src/logger.js +++ /dev/null @@ -1,91 +0,0 @@ -'use strict'; - -/** - * Log as JSON to stdout/stderr. - */ - -const common = require('./common'); - -// This is uncomfortable, but is the simplest way to let logging work for BigInts. -// TODO: revisit with better solution -BigInt.prototype.toJSON = function() { - return this.toString(); -} - -// Also uncomfortable, but let us log Errors reasonably. -Object.defineProperty(Error.prototype, 'toJSON', { - configurable: true, - writable: true, // Required to let Axios override on its own Errors - value: function () { - const result = {}; - const dupKey = function (key) { - // eslint-disable-next-line security/detect-object-injection - result[key] = this[key]; - }; - Object.getOwnPropertyNames(this) - // .filter((prop) => !(prop in [])) - .forEach(dupKey, this); - return result; - }, -}); - -class Logger { - /** - * Wrap backend calls with payload normalization. - * @param {Object} options - * @param {*} backend Console style interface - * @param {Object} options.logger - * @param {String} options.logger.ignoreBelowLevel minimum level to log - * @param {String} options.nodeId - */ - constructor(options, backend = console) { - const logLevels = Object.keys(common.nullLogger); - const ignoreBelowLevel = options && options.logger && options.logger.ignoreBelowLevel || 'debug'; - this.nodeId = options.nodeId; - - if (!logLevels.includes(ignoreBelowLevel)) { - throw new RangeError(`unrecognized minimum log level '${ignoreBelowLevel}'`); - } - const ignoreLevelIdx = logLevels.indexOf(ignoreBelowLevel); - logLevels.forEach((level) => { - // eslint-disable-next-line security/detect-object-injection - this[level] = (logLevels.indexOf(level) > ignoreLevelIdx) ? - common.nop : - this.levelTemplateFn(backend, level); - }); - } - - levelTemplateFn(backend, level) { - // eslint-disable-next-line security/detect-object-injection - if (!(level in backend) || typeof backend[level] !== 'function') { - return common.nop; - } - - // eslint-disable-next-line security/detect-object-injection - return (...args) => backend[level](this.payload(level, ...args)); - } - - payload(level, scope, message, data, ...other) { - // Try to keep credentials out of logs. - // This approach feels sort of jank, but it's better than nothing, for now. - if (data && data.ctx && data.ctx.parsedBody && data.ctx.parsedBody.credential) { - // Create copy of data - data = JSON.parse(JSON.stringify(data)); - data.ctx.parsedBody.credential = '*'.repeat(data.ctx.parsedBody.credential.length); - } - - const now = new Date(); - return JSON.stringify({ - nodeId: this.nodeId, - timestamp: now.toISOString(), - timestampMs: now.getTime(), - level: level, - scope: scope || '[unknown]', - message: message || '', - data: data || {}, - ...(other.length && { other }), - }); - } -} - -module.exports = Logger;