Spaces:
Sleeping
Sleeping
File size: 2,120 Bytes
a218835 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | /**
* Lightweight structured logger for client-side ('use client') components.
*
* Mirrors pino's API surface (info/warn/error/debug) so call sites are
* consistent with the server-side logger in src/lib/logger.ts.
* In production builds, debug and info messages are suppressed.
*/
type LogLevel = 'debug' | 'info' | 'warn' | 'error'
const LOG_LEVELS: Record<LogLevel, number> = {
debug: 10,
info: 20,
warn: 30,
error: 40,
}
const minLevel: number =
process.env.NODE_ENV === 'production' ? LOG_LEVELS.warn : LOG_LEVELS.debug
function shouldLog(level: LogLevel): boolean {
return LOG_LEVELS[level] >= minLevel
}
function formatArgs(
level: LogLevel,
module: string,
msgOrObj: unknown,
...rest: unknown[]
): unknown[] {
const prefix = `[${level.toUpperCase()}] ${module}:`
if (typeof msgOrObj === 'string') {
return [prefix, msgOrObj, ...rest]
}
return [prefix, msgOrObj, ...rest]
}
export interface ClientLogger {
debug(msg: string, ...args: unknown[]): void
debug(obj: Record<string, unknown>, msg?: string): void
info(msg: string, ...args: unknown[]): void
info(obj: Record<string, unknown>, msg?: string): void
warn(msg: string, ...args: unknown[]): void
warn(obj: Record<string, unknown>, msg?: string): void
error(msg: string, ...args: unknown[]): void
error(obj: Record<string, unknown>, msg?: string): void
}
export function createClientLogger(module: string): ClientLogger {
return {
debug(msgOrObj: unknown, ...rest: unknown[]) {
if (!shouldLog('debug')) return
console.debug(...formatArgs('debug', module, msgOrObj, ...rest))
},
info(msgOrObj: unknown, ...rest: unknown[]) {
if (!shouldLog('info')) return
console.info(...formatArgs('info', module, msgOrObj, ...rest))
},
warn(msgOrObj: unknown, ...rest: unknown[]) {
if (!shouldLog('warn')) return
console.warn(...formatArgs('warn', module, msgOrObj, ...rest))
},
error(msgOrObj: unknown, ...rest: unknown[]) {
if (!shouldLog('error')) return
console.error(...formatArgs('error', module, msgOrObj, ...rest))
},
}
}
|