feat(ui): add structured JSON logging to all server-side routes and lib

Introduces a logger.ts module emitting slog-compatible JSON lines to stderr.
Replaces silent catch blocks and console.error calls throughout minio.ts,
pocketbase.ts, hooks.server.ts, login, books, browse, and all API routes so
auth/registration failures, MinIO presign errors, and scraper proxy failures
are now visible in container logs.
This commit is contained in:
Admin
2026-03-03 14:34:48 +05:00
parent 5131ae0bc4
commit bf5774d8d0
13 changed files with 188 additions and 37 deletions

View File

@@ -1,6 +1,7 @@
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { env } from '$env/dynamic/private';
import { log } from '$lib/server/logger';
const SCRAPER_URL = env.SCRAPER_API_URL ?? 'http://localhost:8080';
@@ -35,6 +36,7 @@ export const POST: RequestHandler = async ({ params, request }) => {
if (!scraperRes.ok) {
const text = await scraperRes.text().catch(() => '');
log.error('audio', 'scraper audio generation failed', { slug, chapter, status: scraperRes.status, body: text });
error(scraperRes.status as Parameters<typeof error>[0], text || 'Audio generation failed');
}
@@ -78,6 +80,7 @@ export const GET: RequestHandler = async ({ params, url }) => {
const scraperRes = await fetch(`${SCRAPER_URL}/api/audio-proxy/${slug}/${chapter}?${qs.toString()}`);
if (!scraperRes.ok) {
log.error('audio', 'scraper audio proxy failed', { slug, chapter, status: scraperRes.status });
error(scraperRes.status as Parameters<typeof error>[0], 'Audio not found');
}

View File

@@ -1,6 +1,7 @@
import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { presignAudio } from '$lib/server/minio';
import { log } from '$lib/server/logger';
/**
* GET /api/presign/audio?slug=...&n=...&voice=...&speed=...
@@ -21,6 +22,7 @@ export const GET: RequestHandler = async ({ url }) => {
const presignedUrl = await presignAudio(slug, n, voice, speed);
return json({ url: presignedUrl });
} catch (e) {
log.error('presign', 'presign audio failed', { slug, n, err: String(e) });
error(500, `Could not get presigned URL: ${e}`);
}
};

View File

@@ -1,6 +1,7 @@
import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { setProgress } from '$lib/server/pocketbase';
import { log } from '$lib/server/logger';
/**
* POST /api/progress
@@ -14,6 +15,11 @@ export const POST: RequestHandler = async ({ request, locals }) => {
error(400, 'Invalid body — expected { slug, chapter }');
}
await setProgress(locals.sessionId, body.slug, body.chapter);
try {
await setProgress(locals.sessionId, body.slug, body.chapter);
} catch (e) {
log.error('progress', 'setProgress failed', { slug: body.slug, chapter: body.chapter, err: String(e) });
error(500, 'Failed to save progress');
}
return json({ ok: true });
};

View File

@@ -18,6 +18,7 @@
import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { env } from '$env/dynamic/private';
import { log } from '$lib/server/logger';
const SCRAPER_URL = env.SCRAPER_API_URL ?? 'http://localhost:8080';
@@ -39,11 +40,22 @@ export const POST: RequestHandler = async ({ request, locals }) => {
const endpoint = isBookScrape ? '/scrape/book' : '/scrape';
const upstream = `${SCRAPER_URL}${endpoint}`;
const res = await fetch(upstream, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: isBookScrape ? JSON.stringify({ url: body.url }) : undefined
});
let res: Response;
try {
res = await fetch(upstream, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: isBookScrape ? JSON.stringify({ url: body.url }) : undefined
});
} catch (e) {
log.error('scrape', 'scraper proxy network error', { endpoint, err: String(e) });
throw error(502, 'Could not reach scraper');
}
if (!res.ok && res.status >= 500) {
const text = await res.text().catch(() => '');
log.error('scrape', 'scraper returned error', { endpoint, status: res.status, body: text });
}
const data = await res.json().catch(() => ({}));