Spaces:
Running
Running
Download server/logger.ts from evalstate/mcp-webcam: direct link, hf CLI and curl.
- Browser
- Download file 1.57 kB
-
https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/server/logger.ts
- Command line
-
hf download hf://spaces/evalstate/mcp-webcam/server/logger.ts
-
curl -L -o logger.ts https://huggingface.co/spaces/evalstate/mcp-webcam/resolve/main/server/logger.ts
1.57 kB
| export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; | |
| const LEVELS: Record<LogLevel, number> = { | |
| debug: 10, | |
| info: 20, | |
| warn: 30, | |
| error: 40, | |
| silent: 100, | |
| }; | |
| function parseLevel(raw: string | undefined): LogLevel { | |
| if (!raw) return 'info'; | |
| const normalized = raw.trim().toLowerCase(); | |
| if (normalized in LEVELS) { | |
| return normalized as LogLevel; | |
| } | |
| return 'info'; | |
| } | |
| let currentLevel: LogLevel = parseLevel(process.env.MCP_WEBCAM_LOG); | |
| export function setLogLevel(level: LogLevel | string | undefined): void { | |
| currentLevel = parseLevel(level); | |
| } | |
| export function getLogLevel(): LogLevel { | |
| return currentLevel; | |
| } | |
| function write(level: Exclude<LogLevel, 'silent'>, message: string, meta?: unknown): void { | |
| if (LEVELS[level] < LEVELS[currentLevel]) return; | |
| const ts = new Date().toISOString(); | |
| const suffix = meta !== undefined ? ` ${formatMeta(meta)}` : ''; | |
| process.stderr.write(`[${ts}] [${level.toUpperCase()}] ${message}${suffix}\n`); | |
| } | |
| function formatMeta(meta: unknown): string { | |
| if (meta instanceof Error) { | |
| return meta.stack ?? meta.message; | |
| } | |
| try { | |
| return JSON.stringify(meta); | |
| } catch { | |
| return String(meta); | |
| } | |
| } | |
| export const logger = { | |
| debug(message: string, meta?: unknown): void { | |
| write('debug', message, meta); | |
| }, | |
| info(message: string, meta?: unknown): void { | |
| write('info', message, meta); | |
| }, | |
| warn(message: string, meta?: unknown): void { | |
| write('warn', message, meta); | |
| }, | |
| error(message: string, meta?: unknown): void { | |
| write('error', message, meta); | |
| }, | |
| }; | |