UNPKG

@raiden_network/raiden-cli

Version:

Raiden Light Client standalone app with a REST API via HTTP

59 lines (58 loc) 2.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupLoglevel = void 0; const ethers_1 = require("ethers"); const fs_1 = __importDefault(require("fs")); const loglevel_1 = __importDefault(require("loglevel")); const util_1 = __importDefault(require("util")); util_1.default.inspect.defaultOptions.depth = 5; // +3 from default of 2 const redactions = [ [/(\\?["']?access_?token\\?["']?\s*[=:]\s*\\?["']?)[\w-]+(\\?["']?)/gi, '$1<redacted>$2'], [/(\\?["']?secret\\?["']?\s*[=:]\s*)\[[^\]]+\]/gi, '$1["<redacted>"]'], [/(\\?["']?secret\\?["']?\s*[=:]\s*\\?["']?)\w+(\\?["']?)/gi, '$1<redacted>$2'], ]; function redactLogs(text) { if (typeof text === 'string') text = redactions.reduce((text, [re, repl]) => text.replace(re, repl), text); return text; } /** * @param output - Output filepath */ function setupLoglevel(output) { if (output) { util_1.default.inspect.colors = {}; const logfile = fs_1.default.createWriteStream(output, { flags: 'a' }); process.stdout.write = process.stderr.write = (chunk) => { return logfile.write(redactLogs(chunk)); }; } const originalFactory = loglevel_1.default.methodFactory; let first = true; function raidenMethodFactory(methodName, level, loggerName) { const rawMethod = originalFactory.call(this, methodName, level, loggerName); return (...message) => { const prefix = [new Date(Date.now()).toISOString()]; if (first && typeof loggerName === 'string' && loggerName.startsWith('raiden')) { prefix.push('@', loggerName); first = false; } prefix.push(`[${methodName}]`, '=>'); rawMethod.call(this, ...prefix, ...message); }; } raidenMethodFactory.allowOverwrite = true; loglevel_1.default.methodFactory = raidenMethodFactory; loglevel_1.default.setLevel(process.env.NODE_ENV === 'production' ? 'INFO' : 'DEBUG'); } exports.setupLoglevel = setupLoglevel; // better BigNumber inspect representation for logs Object.defineProperty(ethers_1.BigNumber.prototype, util_1.default.inspect.custom, { enumerable: false, value(_, opts) { return `${opts.stylize('BN', 'special')}(${opts.stylize(this.toString(), 'number')})`; }, });