UNPKG

capacitor-native-update

Version:
102 lines 3.52 kB
import { ConfigManager } from './config'; export var LogLevel; (function (LogLevel) { LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG"; LogLevel[LogLevel["INFO"] = 1] = "INFO"; LogLevel[LogLevel["WARN"] = 2] = "WARN"; LogLevel[LogLevel["ERROR"] = 3] = "ERROR"; })(LogLevel || (LogLevel = {})); export class Logger { constructor() { this.configManager = ConfigManager.getInstance(); } static getInstance() { if (!Logger.instance) { Logger.instance = new Logger(); } return Logger.instance; } shouldLog() { return this.configManager.get('enableLogging'); } sanitize(data) { if (typeof data === 'string') { let sanitized = data; // Remove potential file paths sanitized = sanitized.replace(/\/[^\s]+\/([\w.-]+)$/g, '/<path>/$1'); // Remove potential URLs with credentials sanitized = sanitized.replace(/https?:\/\/[^:]+:[^@]+@/g, 'https://***:***@'); // Remove potential API keys sanitized = sanitized.replace(/[a-zA-Z0-9]{32,}/g, '<redacted>'); return sanitized; } else if (typeof data === 'object' && data !== null) { if (Array.isArray(data)) { return data.map(item => this.sanitize(item)); } else { const sanitized = {}; const dataObj = data; for (const key in dataObj) { if (key.toLowerCase().includes('key') || key.toLowerCase().includes('secret') || key.toLowerCase().includes('password') || key.toLowerCase().includes('token')) { sanitized[key] = '<redacted>'; } else { sanitized[key] = this.sanitize(dataObj[key]); } } return sanitized; } } return data; } log(level, message, data) { if (!this.shouldLog()) return; const timestamp = new Date().toISOString(); const sanitizedData = data ? this.sanitize(data) : undefined; const logEntry = { timestamp, level: LogLevel[level], message, }; if (sanitizedData !== undefined) { logEntry.data = sanitizedData; } switch (level) { case LogLevel.DEBUG: console.debug('[CapacitorNativeUpdate]', logEntry); break; case LogLevel.INFO: console.info('[CapacitorNativeUpdate]', logEntry); break; case LogLevel.WARN: console.warn('[CapacitorNativeUpdate]', logEntry); break; case LogLevel.ERROR: console.error('[CapacitorNativeUpdate]', logEntry); break; } } debug(message, data) { this.log(LogLevel.DEBUG, message, data); } info(message, data) { this.log(LogLevel.INFO, message, data); } warn(message, data) { this.log(LogLevel.WARN, message, data); } error(message, error) { const errorData = error instanceof Error ? { name: error.name, message: error.message, stack: error.stack, } : error; this.log(LogLevel.ERROR, message, errorData); } } //# sourceMappingURL=logger.js.map