UNPKG

react-native-healthkit-bridge

Version:

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

147 lines (115 loc) 4 kB
import { HEALTHKIT_CONFIG } from '../config/healthkit.config'; export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; export interface LogEntry { level: LogLevel; message: string; context?: any; timestamp: string; operation?: string; } export class HealthKitLogger { private static instance: HealthKitLogger; private logs: LogEntry[] = []; private maxLogs = 1000; private constructor() {} static getInstance(): HealthKitLogger { if (!HealthKitLogger.instance) { HealthKitLogger.instance = new HealthKitLogger(); } return HealthKitLogger.instance; } debug(message: string, context?: any, operation?: string): void { this.log('debug', message, context, operation); } info(message: string, context?: any, operation?: string): void { this.log('info', message, context, operation); } warn(message: string, context?: any, operation?: string): void { this.log('warn', message, context, operation); } error(message: string, context?: any, operation?: string): void { this.log('error', message, context, operation); } private log(level: LogLevel, message: string, context?: any, operation?: string): void { if (!HEALTHKIT_CONFIG.LOGGING_ENABLED) return; // Check if we should log this level const levels: LogLevel[] = ['debug', 'info', 'warn', 'error']; const currentLevelIndex = levels.indexOf(HEALTHKIT_CONFIG.LOG_LEVEL); const messageLevelIndex = levels.indexOf(level); if (messageLevelIndex < currentLevelIndex) return; const logEntry: 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?: LogLevel): LogEntry[] { if (level) { return this.logs.filter(log => log.level === level); } return [...this.logs]; } getLogsByOperation(operation: string): LogEntry[] { return this.logs.filter(log => log.operation === operation); } getRecentLogs(count: number = 50): LogEntry[] { return this.logs.slice(-count); } clearLogs(): void { this.logs = []; } getLogStats(): { total: number; byLevel: Record<LogLevel, number> } { const byLevel: Record<LogLevel, number> = { 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: string, message: string, context?: any): void { this.info(message, context, operation); } logError(operation: string, error: any, context?: any): void { this.error(error?.message || 'Unknown error', { error, ...context }, operation); } logPerformance(operation: string, duration: number, context?: any): void { this.info(`Operation completed in ${duration}ms`, { duration, ...context }, operation); } } // Convenience functions export const logger = HealthKitLogger.getInstance(); export function logDebug(message: string, context?: any, operation?: string): void { logger.debug(message, context, operation); } export function logInfo(message: string, context?: any, operation?: string): void { logger.info(message, context, operation); } export function logWarn(message: string, context?: any, operation?: string): void { logger.warn(message, context, operation); } export function logError(message: string, context?: any, operation?: string): void { logger.error(message, context, operation); }