/** * Tiny namespace + level logger for the API layer. * * Default level is `info` — matches the historical "log heavily" behavior of the * fat services. Set `VITE_LOG_LEVEL=warn` (or `error` / `debug` / `silent`) in * `.env` / `.env.local` to tune without rebuilding. * * Usage: * import logger from '@/api/logger' * logger.info('students loaded', count) * logger.error('request failed', error) * * import { child } from '@/api/logger' * const log = child('taskService') * log.debug('fetching page', page) */ const LEVELS = { debug: 10, info: 20, warn: 30, error: 40, silent: 99 } const envLevel = (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env.VITE_LOG_LEVEL) || 'info' const threshold = LEVELS[envLevel] ?? LEVELS.info function fmt(ns, level, args) { const ts = new Date().toISOString().slice(11, 23) return [`[${ts}] [${level.toUpperCase()}] [${ns}]`, ...args] } function makeLogger(ns) { return { debug: (...args) => { if (LEVELS.debug >= threshold) console.debug(...fmt(ns, 'debug', args)) }, info: (...args) => { if (LEVELS.info >= threshold) console.info(...fmt(ns, 'info', args)) }, warn: (...args) => { if (LEVELS.warn >= threshold) console.warn(...fmt(ns, 'warn', args)) }, error: (...args) => { if (LEVELS.error >= threshold) console.error(...fmt(ns, 'error', args)) }, } } export function child(ns) { return makeLogger(ns) } export default makeLogger('api')