UNPKG

native-update

Version:

Foundation package for building a comprehensive update system for Capacitor apps. Provides architecture and interfaces but requires backend implementation.

109 lines 3.77 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(context) { this.configManager = ConfigManager.getInstance(); this.context = context || 'NativeUpdate'; } 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(message, data) { this.logWithLevel(LogLevel.INFO, message, data); } logWithLevel(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], context: this.context, message, }; if (sanitizedData !== undefined) { logEntry.data = sanitizedData; } switch (level) { case LogLevel.DEBUG: console.debug(`[${this.context}]`, logEntry); break; case LogLevel.INFO: console.info(`[${this.context}]`, logEntry); break; case LogLevel.WARN: console.warn(`[${this.context}]`, logEntry); break; case LogLevel.ERROR: console.error(`[${this.context}]`, logEntry); break; } } debug(message, data) { this.logWithLevel(LogLevel.DEBUG, message, data); } info(message, data) { this.logWithLevel(LogLevel.INFO, message, data); } warn(message, data) { this.logWithLevel(LogLevel.WARN, message, data); } error(message, error) { const errorData = error instanceof Error ? { name: error.name, message: error.message, stack: error.stack, } : error; this.logWithLevel(LogLevel.ERROR, message, errorData); } } //# sourceMappingURL=logger.js.map