|
| 1 | +import type { LogLevel } from '../enums/LogLevel.enum'; |
| 2 | + |
| 3 | +export type LoggerFunction = (level: LogLevel, message: string) => void; |
| 4 | + |
| 5 | +const defaultLogger: LoggerFunction = (_level: LogLevel, message: string): void => { |
| 6 | + console.log(`[CodePush] ${message}`); |
| 7 | +}; |
| 8 | + |
| 9 | +let currentLogger: LoggerFunction = defaultLogger; |
| 10 | + |
| 11 | +/** |
| 12 | + * Set a custom logger function to handle all CodePush logs |
| 13 | + * |
| 14 | + * @param logger A function that takes a log level and message and handles the logging |
| 15 | + * @example |
| 16 | + * ``` |
| 17 | + * import { setLogger, LogLevel } from '@appzung/react-native-code-push'; |
| 18 | + * |
| 19 | + * // Custom logger that sends critical logs to a crash reporting service |
| 20 | + * setLogger((level, message) => { |
| 21 | + * // Always log to console |
| 22 | + * console.log(`[CodePush] ${message}`); |
| 23 | + * |
| 24 | + * // Send error logs to crash reporting |
| 25 | + * if (level === LogLevel.ERROR) { |
| 26 | + * MyCrashReportingService.log(`CodePush error: ${message}`); |
| 27 | + * } |
| 28 | + * }); |
| 29 | + * ``` |
| 30 | + */ |
| 31 | +export const setLogger = (logger: LoggerFunction): void => { |
| 32 | + currentLogger = logger; |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * Get the current logger function |
| 37 | + * @returns The current logger function |
| 38 | + */ |
| 39 | +export const getLogger = (): LoggerFunction => currentLogger; |
| 40 | + |
| 41 | +/** |
| 42 | + * Reset the logger to the default implementation |
| 43 | + */ |
| 44 | +export const resetLogger = (): void => { |
| 45 | + currentLogger = defaultLogger; |
| 46 | +}; |
0 commit comments