UNPKG

react-native-healthkit-bridge

Version:

A comprehensive React Native bridge for Apple HealthKit with TypeScript support, advanced authorization, and flexible data queries

111 lines (110 loc) 3.6 kB
import { HEALTHKIT_CONFIG } from '../config/healthkit.config'; export class HealthKitLogger { constructor() { this.logs = []; this.maxLogs = 1000; } static getInstance() { if (!HealthKitLogger.instance) { HealthKitLogger.instance = new HealthKitLogger(); } return HealthKitLogger.instance; } debug(message, context, operation) { this.log('debug', message, context, operation); } info(message, context, operation) { this.log('info', message, context, operation); } warn(message, context, operation) { this.log('warn', message, context, operation); } error(message, context, operation) { this.log('error', message, context, operation); } log(level, message, context, operation) { if (!HEALTHKIT_CONFIG.LOGGING_ENABLED) return; // Check if we should log this level const levels = ['debug', 'info', 'warn', 'error']; const currentLevelIndex = levels.indexOf(HEALTHKIT_CONFIG.LOG_LEVEL); const messageLevelIndex = levels.indexOf(level); if (messageLevelIndex < currentLevelIndex) return; const logEntry = { level, message, context, timestamp: new Date().toISOString(), operation }; this.logs.push(logEntry); // Limit log size if (this.logs.length > this.maxLogs) { this.logs = this.logs.slice(-this.maxLogs); } // Console output const prefix = `[HealthKit ${level.toUpperCase()}]`; const operationPrefix = operation ? `[${operation}] ` : ''; if (context) { console.log(`${prefix} ${operationPrefix}${message}`, context); } else { console.log(`${prefix} ${operationPrefix}${message}`); } } getLogs(level) { if (level) { return this.logs.filter(log => log.level === level); } return [...this.logs]; } getLogsByOperation(operation) { return this.logs.filter(log => log.operation === operation); } getRecentLogs(count = 50) { return this.logs.slice(-count); } clearLogs() { this.logs = []; } getLogStats() { const byLevel = { debug: 0, info: 0, warn: 0, error: 0 }; this.logs.forEach(log => { byLevel[log.level]++; }); return { total: this.logs.length, byLevel }; } // Helper methods for common operations logOperation(operation, message, context) { this.info(message, context, operation); } logError(operation, error, context) { this.error((error === null || error === void 0 ? void 0 : error.message) || 'Unknown error', Object.assign({ error }, context), operation); } logPerformance(operation, duration, context) { this.info(`Operation completed in ${duration}ms`, Object.assign({ duration }, context), operation); } } // Convenience functions export const logger = HealthKitLogger.getInstance(); export function logDebug(message, context, operation) { logger.debug(message, context, operation); } export function logInfo(message, context, operation) { logger.info(message, context, operation); } export function logWarn(message, context, operation) { logger.warn(message, context, operation); } export function logError(message, context, operation) { logger.error(message, context, operation); }