UNPKG

humanbehavior-js

Version:

SDK for HumanBehavior session and event recording

144 lines (113 loc) 3.88 kB
export enum LogLevel { NONE = 0, ERROR = 1, WARN = 2, INFO = 3, DEBUG = 4 } export interface LoggerConfig { level: LogLevel; enableConsole: boolean; enableStorage: boolean; } class Logger { private config: LoggerConfig = { level: LogLevel.ERROR, // Default to only errors in production enableConsole: true, enableStorage: false }; private isBrowser = typeof window !== 'undefined'; constructor(config?: Partial<LoggerConfig>) { if (config) { this.config = { ...this.config, ...config }; } } setConfig(config: Partial<LoggerConfig>): void { this.config = { ...this.config, ...config }; } private shouldLog(level: LogLevel): boolean { return level <= this.config.level; } private formatMessage(level: string, message: string, ...args: any[]): string { const timestamp = new Date().toISOString(); return `[HumanBehavior ${level}] ${timestamp}: ${message}`; } error(message: string, ...args: any[]): void { if (!this.shouldLog(LogLevel.ERROR)) return; const formattedMessage = this.formatMessage('ERROR', message); if (this.config.enableConsole) { console.error(formattedMessage, ...args); } if (this.config.enableStorage && this.isBrowser) { this.logToStorage(formattedMessage, args); } } warn(message: string, ...args: any[]): void { if (!this.shouldLog(LogLevel.WARN)) return; const formattedMessage = this.formatMessage('WARN', message); if (this.config.enableConsole) { console.warn(formattedMessage, ...args); } if (this.config.enableStorage && this.isBrowser) { this.logToStorage(formattedMessage, args); } } info(message: string, ...args: any[]): void { if (!this.shouldLog(LogLevel.INFO)) return; const formattedMessage = this.formatMessage('INFO', message); if (this.config.enableConsole) { console.log(formattedMessage, ...args); } if (this.config.enableStorage && this.isBrowser) { this.logToStorage(formattedMessage, args); } } debug(message: string, ...args: any[]): void { if (!this.shouldLog(LogLevel.DEBUG)) return; const formattedMessage = this.formatMessage('DEBUG', message); if (this.config.enableConsole) { console.log(formattedMessage, ...args); } if (this.config.enableStorage && this.isBrowser) { this.logToStorage(formattedMessage, args); } } private logToStorage(message: string, args: any[]): void { try { const logs = JSON.parse(localStorage.getItem('human_behavior_logs') || '[]'); const logEntry = { message, args: args.length > 0 ? args : undefined, timestamp: Date.now() }; logs.push(logEntry); // Keep only last 1000 logs to prevent storage bloat if (logs.length > 1000) { logs.splice(0, logs.length - 1000); } localStorage.setItem('human_behavior_logs', JSON.stringify(logs)); } catch (e) { // Silently fail if storage is not available } } getLogs(): any[] { if (!this.isBrowser) return []; try { return JSON.parse(localStorage.getItem('human_behavior_logs') || '[]'); } catch (e) { return []; } } clearLogs(): void { if (this.isBrowser) { localStorage.removeItem('human_behavior_logs'); } } } // Create singleton instance export const logger = new Logger(); // Export convenience methods export const logError = (message: string, ...args: any[]) => logger.error(message, ...args); export const logWarn = (message: string, ...args: any[]) => logger.warn(message, ...args); export const logInfo = (message: string, ...args: any[]) => logger.info(message, ...args); export const logDebug = (message: string, ...args: any[]) => logger.debug(message, ...args);