UNPKG

@actyx/sdk

Version:
98 lines 3.48 kB
"use strict"; /* * Actyx SDK: Functions for writing distributed apps * deployed on peer-to-peer networks, without any servers. * * Copyright (C) 2021 Actyx AG */ /* eslint-disable @typescript-eslint/no-explicit-any */ Object.defineProperty(exports, "__esModule", { value: true }); exports.enableAllLoggersExcept = exports.makeLogPattern = exports.Loggers = exports.LoggersInternal = exports.globalLogLeech = exports.mkLoggers = exports.mkLogger = exports.mkTestLoggers = void 0; const debug = require("debug"); function mkTestLogger(dump) { function logger(...args) { dump.push(args.map((x) => JSON.stringify(x)).join(':')); } logger.namespace = 'test'; logger.enabled = true; return logger; } const mkTestLoggers = () => { const errors = []; const warnings = []; return { errors, warnings, error: mkTestLogger(errors), warn: mkTestLogger(warnings), info: mkTestLogger([]), debug: mkTestLogger([]), }; }; exports.mkTestLoggers = mkTestLoggers; // The goal is to make our logger look exactly like one from the 'debug' library, // only we potentially leech the inputs - before they are formatted! const mkLogger = (topic, logFnOverride) => { const actualLogger = debug(topic); if (logFnOverride) { actualLogger.log = logFnOverride; } const logger = (first, ...rest) => { if (actualLogger.enabled) { actualLogger(first, ...rest); try { exports.LoggersInternal.globalLogLeech(actualLogger.namespace, first, ...rest); } catch (e) { actualLogger('Error while leeching log message: ', e); } } }; // Easiest way to supply the readonly namespace/enabled properties required by the interface. Object.setPrototypeOf(logger, actualLogger); return logger; }; exports.mkLogger = mkLogger; // todo: special treatment for errors? const mkLoggers = (topic) => ({ error: (0, exports.mkLogger)(`${topic}:error`), warn: (0, exports.mkLogger)(`${topic}:warn`), info: (0, exports.mkLogger)(`${topic}:info`), debug: (0, exports.mkLogger)(`${topic}:debug`), }); exports.mkLoggers = mkLoggers; const globalLogLeech = () => { /* Nothing by default. Overridden by monitoring module. */ /** * If you want to add another global log consumer, * consider extending the API here to hold any number * of consumers that are each called for every log invocation... * like more extensive logging frameworks let you. */ }; exports.globalLogLeech = globalLogLeech; exports.LoggersInternal = { globalLogLeech: exports.globalLogLeech, testLoggers: exports.mkTestLoggers, }; /** Loggers associated methods. * @public */ exports.Loggers = { of: exports.mkLoggers, }; /** * Build logging pattern for consumption by the `debug` library. * @public */ const makeLogPattern = (excludeModules) => `*,${excludeModules.map((x) => `-${x}:((?!error).)*`).join(',')},*:error`; exports.makeLogPattern = makeLogPattern; /** * Utility function to enable all logging with exception for passed in logger namespaces. * For excluded logger namespaces errors will still be logged! * @public */ const enableAllLoggersExcept = (excludeModules) => { // $ExpectError localStorage.debug = (0, exports.makeLogPattern)(excludeModules); }; exports.enableAllLoggersExcept = enableAllLoggersExcept; //# sourceMappingURL=logging.js.map