|
| 1 | +/** |
| 2 | + * Log level type for categorizing log messages. |
| 3 | + * |
| 4 | + * Includes standard console methods supported in both browser and Node.js: |
| 5 | + * - Standard levels: `log`, `warn`, `error`, `info` |
| 6 | + * - Performance timing: `time`, `timeEnd` |
| 7 | + */ |
| 8 | +export type LogType = 'log' | 'warn' | 'error' | 'info' | 'time' | 'timeEnd'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Logger function interface for environment-specific logging implementations. |
| 12 | + * |
| 13 | + * Implementations should handle message formatting, output styling, |
| 14 | + * and platform-specific logging mechanisms (e.g., console, file, network). |
| 15 | + * |
| 16 | + * @param msg - The message to log. |
| 17 | + * @param type - Log level/severity (default: 'log'). |
| 18 | + * @param args - Additional data to include with the log message. |
| 19 | + */ |
| 20 | +export interface Logger { |
| 21 | + (msg: string, type?: LogType, args?: unknown): void; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Global logger instance, set by environment-specific packages. |
| 26 | + */ |
| 27 | +let loggerInstance: Logger | null = null; |
| 28 | + |
| 29 | +/** |
| 30 | + * Registers the environment-specific logger implementation. |
| 31 | + * |
| 32 | + * This should be called once during application initialization |
| 33 | + * by the environment-specific package. |
| 34 | + * |
| 35 | + * @param logger - Logger implementation to use globally. |
| 36 | + */ |
| 37 | +export function setLogger(logger: Logger): void { |
| 38 | + loggerInstance = logger; |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Logs a message using the registered logger implementation. |
| 43 | + * |
| 44 | + * If no logger has been registered via {@link setLogger}, this is a no-op. |
| 45 | + * |
| 46 | + * @param msg - Message to log. |
| 47 | + * @param type - Log level (default: 'log'). |
| 48 | + * @param args - Additional arguments to log. |
| 49 | + */ |
| 50 | +export function log(msg: string, type?: LogType, args?: unknown): void { |
| 51 | + if (loggerInstance) { |
| 52 | + loggerInstance(msg, type, args); |
| 53 | + } |
| 54 | +} |
0 commit comments