21 lines
934 B
TypeScript
21 lines
934 B
TypeScript
import { sanitize } from './sanitize'
|
|
|
|
export type LogContext = Record<string, unknown>
|
|
type Level = 'debug' | 'info' | 'warn' | 'error'
|
|
|
|
const weights: Record<Level, number> = { debug: 10, info: 20, warn: 30, error: 40 }
|
|
const configuredLevel = (import.meta.env.VITE_LOG_LEVEL || 'info') as Level
|
|
|
|
function write(level: Level, message: string, context: LogContext = {}) {
|
|
if (weights[level] < (weights[configuredLevel] ?? weights.info)) return
|
|
const entry = sanitize({ timestamp: new Date().toISOString(), level, message, ...context })
|
|
console[level](entry)
|
|
}
|
|
|
|
export const logger = {
|
|
debug: (message: string, context?: LogContext) => write('debug', message, context),
|
|
info: (message: string, context?: LogContext) => write('info', message, context),
|
|
warn: (message: string, context?: LogContext) => write('warn', message, context),
|
|
error: (message: string, context?: LogContext) => write('error', message, context),
|
|
}
|