UNPKG

@ackplus/react-tanstack-data-table

Version:

A powerful React data table component built with MUI and TanStack Table

127 lines (126 loc) 4.78 kB
"use strict"; /** * Logging utilities for the DataTable package. * * Provides a lightweight wrapper around `console` that can be configured globally * or per-instance to help troubleshoot behaviour in consuming applications. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getDataTableLoggingConfig = exports.configureDataTableLogging = exports.createLogger = void 0; const LOG_LEVEL_ORDER = { silent: 0, error: 1, warn: 2, info: 3, debug: 4, }; const defaultConsole = typeof console !== 'undefined' ? console : { log: () => undefined, debug: () => undefined, info: () => undefined, warn: () => undefined, error: () => undefined, }; let globalConfig = { enabled: false, level: 'warn', prefix: 'DataTable', scope: '', includeTimestamp: false, logger: defaultConsole, }; const isLevelEnabled = (level, config) => { if (!config.enabled) { return false; } return LOG_LEVEL_ORDER[level] <= LOG_LEVEL_ORDER[config.level]; }; const formatPrefix = (level, config) => { const segments = []; if (config.prefix) { segments.push(config.prefix); } if (config.scope) { segments.push(config.scope); } const prefix = segments.length > 0 ? `[${segments.join(':')}]` : ''; return config.includeTimestamp ? `[${new Date().toISOString()}]${prefix ? ` ${prefix}` : ''} [${level.toUpperCase()}]` : `${prefix ? `${prefix} ` : ''}[${level.toUpperCase()}]`; }; const getConsoleMethod = (level, logger) => { var _a, _b, _c; const methodName = level === 'debug' ? 'debug' : level; return (_c = (_b = (_a = logger[methodName]) !== null && _a !== void 0 ? _a : logger.log) !== null && _b !== void 0 ? _b : defaultConsole.log) !== null && _c !== void 0 ? _c : (() => undefined); }; const normaliseInput = (input) => { if (typeof input === 'boolean') { return { enabled: input }; } return input !== null && input !== void 0 ? input : {}; }; const resolveConfig = (scope, input, parent) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j; const overrides = normaliseInput(input); const base = parent !== null && parent !== void 0 ? parent : globalConfig; return { enabled: (_a = overrides.enabled) !== null && _a !== void 0 ? _a : base.enabled, level: (_b = overrides.level) !== null && _b !== void 0 ? _b : base.level, prefix: (_c = overrides.prefix) !== null && _c !== void 0 ? _c : base.prefix, scope: (_f = (_e = (_d = overrides.scope) !== null && _d !== void 0 ? _d : scope) !== null && _e !== void 0 ? _e : base.scope) !== null && _f !== void 0 ? _f : '', includeTimestamp: (_g = overrides.includeTimestamp) !== null && _g !== void 0 ? _g : base.includeTimestamp, logger: (_j = (_h = overrides.logger) !== null && _h !== void 0 ? _h : base.logger) !== null && _j !== void 0 ? _j : defaultConsole, }; }; const createLoggerMethods = (config) => { const logWithLevel = (level) => { const consoleMethod = getConsoleMethod(level, config.logger); return (...args) => { if (!isLevelEnabled(level, config)) { return; } const prefix = formatPrefix(level, config); consoleMethod(prefix, ...args); }; }; return { debug: logWithLevel('debug'), info: logWithLevel('info'), warn: logWithLevel('warn'), error: logWithLevel('error'), }; }; /** * Create a new logger instance. Configuration cascades from the global config unless overridden. */ const createLogger = (scope, input, parentConfig) => { const resolvedConfig = resolveConfig(scope, input, parentConfig); const methods = createLoggerMethods(resolvedConfig); const child = (childScope, overrides) => { const combinedScope = childScope ? (resolvedConfig.scope ? `${resolvedConfig.scope}.${childScope}` : childScope) : resolvedConfig.scope; return (0, exports.createLogger)(combinedScope, overrides, resolvedConfig); }; return { ...methods, child, isLevelEnabled: (level) => isLevelEnabled(level, resolvedConfig), config: resolvedConfig, }; }; exports.createLogger = createLogger; /** * Configure the global logger defaults for every DataTable instance. */ const configureDataTableLogging = (options) => { globalConfig = resolveConfig(options.scope, options, globalConfig); }; exports.configureDataTableLogging = configureDataTableLogging; /** * Read the current global logging configuration. */ const getDataTableLoggingConfig = () => ({ ...globalConfig }); exports.getDataTableLoggingConfig = getDataTableLoggingConfig;