UNPKG

@splitsoftware/splitio-commons

Version:
48 lines (47 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateLogger = void 0; var logger_1 = require("../../../logger"); var isLocalStorageAvailable_1 = require("../../env/isLocalStorageAvailable"); var isNode_1 = require("../../env/isNode"); var debug_1 = require("../../../logger/messages/debug"); var commons_1 = require("./commons"); var allCodes = new Map(debug_1.codesDebug); // @TODO set default debug setting instead of initialLogLevel when integrating in JS and Node packages var LS_KEY = 'splitio_debug'; var ENV_VAR_KEY = 'SPLITIO_DEBUG'; /** * Logger initial debug level, that is disabled ('NONE') by default. * Acceptable values are: 'DEBUG', 'INFO', 'WARN', 'ERROR', 'NONE'. * Other acceptable values are 'on', 'enable' and 'enabled', which are equivalent to 'DEBUG'. * Any other string value is equivalent to disable. */ var initialState = String( // eslint-disable-next-line no-undef isNode_1.isNode ? process.env[ENV_VAR_KEY] : (0, isLocalStorageAvailable_1.isLocalStorageAvailable)() ? localStorage.getItem(LS_KEY) : ''); // By default it starts disabled. var initialLogLevel = logger_1.LogLevels.NONE; // Kept to avoid a breaking change ('on', 'enable' and 'enabled' are equivalent) if (/^(enabled?|on)/i.test(initialState)) { initialLogLevel = logger_1.LogLevels.DEBUG; } else if ((0, logger_1.isLogLevelString)(initialState)) { initialLogLevel = initialState; } /** * Validates the `debug` property at config and use it to set the log level. * * @param settings - user config object, with an optional `debug` property of type boolean or string log level. * @returns a logger instance with the log level at `settings.debug`. If `settings.debug` is invalid or not provided, `initialLogLevel` is used. */ function validateLogger(settings) { var debug = settings.debug, logger = settings.logger; var logLevel = debug !== undefined ? (0, commons_1.getLogLevel)(debug) : initialLogLevel; var log = new logger_1.Logger({ logLevel: logLevel || initialLogLevel }, allCodes); log.setLogger(logger); // if logLevel is undefined at this point, it means that settings `debug` value is invalid if (!logLevel) log._log('error', 'Invalid Log Level - No changes to the logs will be applied.'); return log; } exports.validateLogger = validateLogger;