UNPKG

flipper-common

Version:

Server & UI shared Flipper utilities

88 lines 2.65 kB
"use strict"; /** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LoggerFormat = exports.LoggerExtractError = exports.NoopLogger = exports.setLoggerInstance = exports.getLogger = void 0; const isTest_1 = require("../utils/isTest"); const errors_1 = require("../utils/errors"); let instance = null; function getLogger() { if (instance) { return instance; } if ((0, isTest_1.isTest)()) { instance = new NoopLogger(); return instance; } throw new Error('Requested Logger instance without initializing it. Make sure init() is called at app start'); } exports.getLogger = getLogger; // only for testing function setLoggerInstance(logger) { instance = logger; } exports.setLoggerInstance = setLoggerInstance; /** * A logger that doesn't log anything, used in tests and public Flipper builds */ class NoopLogger { constructor() { } track(_type, _event, _data, _plugin) { } trackTimeSince(_mark, _eventName) { } info(_data, _category) { } warn(_data, _category) { } error(_data, _category) { } debug(_data, _category) { } } exports.NoopLogger = NoopLogger; function LoggerExtractError(...data) { const message = (0, errors_1.getStringFromErrorLike)(data); const error = (0, errors_1.getErrorFromErrorLike)(data) ?? new Error(message); return { message, error, }; } exports.LoggerExtractError = LoggerExtractError; function LoggerFormat(type, ...data) { function stringify(value) { if (value === undefined || value === null || typeof value == 'object' || typeof value == 'function' || typeof value == 'symbol') { try { return JSON.stringify(value, null, 2); } catch (e) { return JSON.stringify({ error: `Failed to serialise: ${e}`, }, null, 2); } } return value; } const date = new Date().toISOString(); const human = 'flipper'; const args = [`[${date}] ${human}`]; if (data) { args[0] += ':'; data.forEach((e) => args.push(e)); } const msg = data.map((e) => stringify(e)).join(' '); return { msg, namespace: 'flipper', time: Date.now(), type, formatted: args, }; } exports.LoggerFormat = LoggerFormat; //# sourceMappingURL=Logger.js.map