Download src/lib/logger.ts from HonzaH/Letschat: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/lib/logger.ts
- Command line
-
hf download hf://spaces/HonzaH/Letschat/src/lib/logger.ts
-
curl -L -o logger.ts https://huggingface.co/spaces/HonzaH/Letschat/resolve/main/src/lib/logger.ts
2.71 kB
| import { Pool } from 'pg' | |
| import { randomUUID } from 'crypto' | |
| import { createServiceClient } from './supabase/server' | |
| type LogLevel = 'all' | 'light' | 'mini' | 'off' | |
| const level = (process.env.LOG_LEVEL as LogLevel) || 'light' | |
| const LOCAL_DB_URL = process.env.LOCAL_DATABASE_URL | |
| let pool: Pool | null = null | |
| let logsTableReady = false | |
| if (LOCAL_DB_URL) { | |
| pool = new Pool({ connectionString: LOCAL_DB_URL }) | |
| } | |
| async function ensureLogsTable() { | |
| if (!pool || logsTableReady) return | |
| try { | |
| await pool.query(` | |
| CREATE TABLE IF NOT EXISTS logs ( | |
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | |
| created_at TIMESTAMP DEFAULT NOW(), | |
| lang VARCHAR, | |
| ip VARCHAR, | |
| user_agent VARCHAR, | |
| module VARCHAR, | |
| operation VARCHAR, | |
| data JSONB, | |
| error TEXT, | |
| level VARCHAR | |
| ); | |
| `) | |
| logsTableReady = true | |
| } catch (e) { | |
| console.error('[logger] Failed to ensure logs table:', e) | |
| } | |
| } | |
| export interface LogPayload { | |
| module: string | |
| operation: string | |
| data?: Record<string, any> | null | |
| error?: string | null | |
| ip?: string | null | |
| userAgent?: string | null | |
| lang?: string | null | |
| } | |
| export async function logEvent(payload: LogPayload) { | |
| if (level === 'off') return | |
| const shouldIncludeUA = level === 'all' | |
| const shouldIncludeData = level === 'all' | |
| const row = { | |
| id: randomUUID(), | |
| created_at: new Date().toISOString(), | |
| lang: payload.lang ?? null, | |
| ip: payload.ip ?? null, | |
| user_agent: shouldIncludeUA ? payload.userAgent ?? null : null, | |
| module: payload.module, | |
| operation: payload.operation, | |
| data: shouldIncludeData ? payload.data ?? null : null, | |
| error: payload.error ?? null, | |
| level | |
| } | |
| // Prefer local DB | |
| if (pool) { | |
| await ensureLogsTable() | |
| try { | |
| await pool.query( | |
| 'INSERT INTO logs(id, created_at, lang, ip, user_agent, module, operation, data, error, level) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)', | |
| [row.id, row.created_at, row.lang, row.ip, row.user_agent, row.module, row.operation, row.data, row.error, row.level] | |
| ) | |
| return | |
| } catch (e) { | |
| console.error('[logger] Failed to insert log into local DB:', e) | |
| } | |
| } | |
| // Fallback to Supabase service-role if available | |
| try { | |
| if (process.env.SUPABASE_SERVICE_ROLE_KEY && process.env.SUPABASE_URL) { | |
| const supabase = await createServiceClient() | |
| // logs table není v generovaných typech; vlož jako any | |
| await supabase.from('logs' as any).insert(row as any) | |
| return | |
| } | |
| } catch (e) { | |
| console.error('[logger] Failed to insert log into Supabase:', e) | |
| } | |
| // Fallback to in-memory/console | |
| console.info('[logger] (fallback)', row) | |
| } | |