mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Implement centralized logging with RMABLogger
Replaces scattered console statements with a unified RMABLogger across backend API routes and services. Adds LOG_LEVEL-based filtering, job-aware database persistence, and context-based logging. Updates documentation to describe the new logging system and usage patterns. Also documents qBittorrent CSRF header fix
This commit is contained in:
@@ -11,8 +11,9 @@ import { prisma } from '@/lib/db';
|
||||
import { downloadEbook } from '@/lib/services/ebook-scraper';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const DEBUG_ENABLED = process.env.LOG_LEVEL === 'debug';
|
||||
const logger = RMABLogger.create('API.FetchEbook');
|
||||
|
||||
/**
|
||||
* Sanitize path component (same logic as file-organizer)
|
||||
@@ -135,19 +136,21 @@ export async function POST(
|
||||
audiobook.audibleAsin
|
||||
);
|
||||
|
||||
if (DEBUG_ENABLED) {
|
||||
console.log(`[FetchEbook] Request: ${id}, Title: "${audiobook.title}", Author: "${audiobook.author}"`);
|
||||
console.log(`[FetchEbook] Target path: ${targetPath}`);
|
||||
console.log(`[FetchEbook] Config: format=${preferredFormat}, baseUrl=${baseUrl}, flaresolverr=${flaresolverrUrl || 'none'}`);
|
||||
}
|
||||
logger.debug('Fetch e-book request', {
|
||||
requestId: id,
|
||||
title: audiobook.title,
|
||||
author: audiobook.author,
|
||||
targetPath,
|
||||
format: preferredFormat,
|
||||
baseUrl,
|
||||
flaresolverr: flaresolverrUrl || 'none'
|
||||
});
|
||||
|
||||
// Check if target directory exists
|
||||
try {
|
||||
await fs.access(targetPath);
|
||||
} catch {
|
||||
if (DEBUG_ENABLED) {
|
||||
console.log(`[FetchEbook] Target directory not found: ${targetPath}`);
|
||||
}
|
||||
logger.debug(`Target directory not found: ${targetPath}`);
|
||||
return NextResponse.json(
|
||||
{ error: 'Audiobook directory not found. Was the audiobook properly organized?' },
|
||||
{ status: 400 }
|
||||
@@ -167,21 +170,21 @@ export async function POST(
|
||||
);
|
||||
|
||||
if (result.success) {
|
||||
console.log(`[FetchEbook] Success: ${result.filePath ? path.basename(result.filePath) : 'unknown'} for "${audiobook.title}"`);
|
||||
logger.info(`E-book downloaded: ${result.filePath ? path.basename(result.filePath) : 'unknown'} for "${audiobook.title}"`);
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `E-book downloaded: ${result.filePath ? path.basename(result.filePath) : 'unknown'}`,
|
||||
format: result.format,
|
||||
});
|
||||
} else {
|
||||
console.log(`[FetchEbook] Failed for "${audiobook.title}": ${result.error}`);
|
||||
logger.warn(`E-book download failed for "${audiobook.title}"`, { error: result.error });
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: result.error || 'E-book download failed',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[FetchEbook] Unexpected error:', error instanceof Error ? error.message : error);
|
||||
logger.error('Unexpected error', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : 'Internal server error' },
|
||||
{ status: 500 }
|
||||
|
||||
@@ -8,6 +8,9 @@ import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { getProwlarrService } from '@/lib/integrations/prowlarr.service';
|
||||
import { rankTorrents } from '@/lib/utils/ranking-algorithm';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.InteractiveSearch');
|
||||
|
||||
/**
|
||||
* POST /api/requests/[id]/interactive-search
|
||||
@@ -96,9 +99,9 @@ export async function POST(
|
||||
// Use custom title if provided, otherwise use audiobook's title
|
||||
const searchQuery = customTitle || requestRecord.audiobook.title;
|
||||
|
||||
console.log(`[InteractiveSearch] Searching ${enabledIndexerIds.length} enabled indexers for: ${searchQuery}`);
|
||||
logger.info(`Searching ${enabledIndexerIds.length} enabled indexers`, { searchQuery });
|
||||
if (customTitle) {
|
||||
console.log(`[InteractiveSearch] Using custom search title (original: "${requestRecord.audiobook.title}")`);
|
||||
logger.debug('Using custom search title', { customTitle, originalTitle: requestRecord.audiobook.title });
|
||||
}
|
||||
|
||||
const results = await prowlarr.search(searchQuery, {
|
||||
@@ -106,7 +109,7 @@ export async function POST(
|
||||
maxResults: 100, // Increased limit for broader search
|
||||
});
|
||||
|
||||
console.log(`[InteractiveSearch] Found ${results.length} raw results for request ${id}`);
|
||||
logger.debug(`Found ${results.length} raw results`, { requestId: id });
|
||||
|
||||
if (results.length === 0) {
|
||||
return NextResponse.json({
|
||||
@@ -125,42 +128,30 @@ export async function POST(
|
||||
|
||||
// No threshold filtering for interactive search - show all results
|
||||
// User can see scores and make their own decision
|
||||
console.log(`[InteractiveSearch] Ranked ${rankedResults.length} results (no threshold filter - user decides)`);
|
||||
logger.debug(`Ranked ${rankedResults.length} results (no threshold filter - user decides)`);
|
||||
|
||||
// Log top 3 results with detailed score breakdown for debugging
|
||||
const top3 = rankedResults.slice(0, 3);
|
||||
if (top3.length > 0) {
|
||||
console.log(`[InteractiveSearch] ==================== RANKING DEBUG ====================`);
|
||||
console.log(`[InteractiveSearch] Search Query: "${searchQuery}"`);
|
||||
console.log(`[InteractiveSearch] Requested Title (for ranking): "${requestRecord.audiobook.title}"`);
|
||||
console.log(`[InteractiveSearch] Requested Author (for ranking): "${requestRecord.audiobook.author}"`);
|
||||
console.log(`[InteractiveSearch] Top ${top3.length} results (out of ${rankedResults.length} total):`);
|
||||
console.log(`[InteractiveSearch] --------------------------------------------------------`);
|
||||
logger.debug('==================== RANKING DEBUG ====================');
|
||||
logger.debug('Search parameters', { searchQuery, requestedTitle: requestRecord.audiobook.title, requestedAuthor: requestRecord.audiobook.author });
|
||||
logger.debug(`Top ${top3.length} results (out of ${rankedResults.length} total)`);
|
||||
logger.debug('--------------------------------------------------------');
|
||||
top3.forEach((result, index) => {
|
||||
console.log(`[InteractiveSearch] ${index + 1}. "${result.title}"`);
|
||||
console.log(`[InteractiveSearch] Indexer: ${result.indexer}${result.indexerId ? ` (ID: ${result.indexerId})` : ''}`);
|
||||
console.log(`[InteractiveSearch] `);
|
||||
console.log(`[InteractiveSearch] Base Score: ${result.score.toFixed(1)}/100`);
|
||||
console.log(`[InteractiveSearch] - Title/Author Match: ${result.breakdown.matchScore.toFixed(1)}/60`);
|
||||
console.log(`[InteractiveSearch] - Format Quality: ${result.breakdown.formatScore.toFixed(1)}/25 (${result.format || 'unknown'})`);
|
||||
console.log(`[InteractiveSearch] - Seeder Count: ${result.breakdown.seederScore.toFixed(1)}/15 (${result.seeders} seeders)`);
|
||||
console.log(`[InteractiveSearch] `);
|
||||
console.log(`[InteractiveSearch] Bonus Points: +${result.bonusPoints.toFixed(1)}`);
|
||||
if (result.bonusModifiers.length > 0) {
|
||||
result.bonusModifiers.forEach(mod => {
|
||||
console.log(`[InteractiveSearch] - ${mod.reason}: +${mod.points.toFixed(1)}`);
|
||||
});
|
||||
}
|
||||
console.log(`[InteractiveSearch] `);
|
||||
console.log(`[InteractiveSearch] Final Score: ${result.finalScore.toFixed(1)}`);
|
||||
if (result.breakdown.notes.length > 0) {
|
||||
console.log(`[InteractiveSearch] Notes: ${result.breakdown.notes.join(', ')}`);
|
||||
}
|
||||
if (index < top3.length - 1) {
|
||||
console.log(`[InteractiveSearch] --------------------------------------------------------`);
|
||||
}
|
||||
logger.debug(`${index + 1}. "${result.title}"`, {
|
||||
indexer: result.indexer,
|
||||
indexerId: result.indexerId,
|
||||
baseScore: `${result.score.toFixed(1)}/100`,
|
||||
matchScore: `${result.breakdown.matchScore.toFixed(1)}/60`,
|
||||
formatScore: `${result.breakdown.formatScore.toFixed(1)}/25 (${result.format || 'unknown'})`,
|
||||
seederScore: `${result.breakdown.seederScore.toFixed(1)}/15 (${result.seeders} seeders)`,
|
||||
bonusPoints: `+${result.bonusPoints.toFixed(1)}`,
|
||||
bonusModifiers: result.bonusModifiers.map(mod => `${mod.reason}: +${mod.points.toFixed(1)}`),
|
||||
finalScore: result.finalScore.toFixed(1),
|
||||
notes: result.breakdown.notes,
|
||||
});
|
||||
});
|
||||
console.log(`[InteractiveSearch] ========================================================`);
|
||||
logger.debug('========================================================');
|
||||
}
|
||||
|
||||
// Add rank position to each result
|
||||
@@ -177,7 +168,7 @@ export async function POST(
|
||||
: 'No results found',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to perform interactive search:', error);
|
||||
logger.error('Failed to perform interactive search', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'SearchError',
|
||||
|
||||
@@ -7,6 +7,9 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { getJobQueueService } from '@/lib/services/job-queue.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.ManualSearch');
|
||||
|
||||
/**
|
||||
* POST /api/requests/[id]/manual-search
|
||||
@@ -89,7 +92,7 @@ export async function POST(
|
||||
message: 'Manual search initiated',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to trigger manual search:', error);
|
||||
logger.error('Failed to trigger manual search', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'SearchError',
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.RequestById');
|
||||
|
||||
/**
|
||||
* GET /api/requests/[id]
|
||||
@@ -70,7 +73,7 @@ export async function GET(
|
||||
request: requestRecord,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to get request:', error);
|
||||
logger.error('Failed to get request', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'FetchError',
|
||||
@@ -304,7 +307,7 @@ export async function PATCH(
|
||||
{ status: 400 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Failed to update request:', error);
|
||||
logger.error('Failed to update request', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'UpdateError',
|
||||
@@ -351,7 +354,7 @@ export async function DELETE(
|
||||
message: 'Request deleted successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to delete request:', error);
|
||||
logger.error('Failed to delete request', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'DeleteError',
|
||||
|
||||
@@ -8,6 +8,9 @@ import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { getJobQueueService } from '@/lib/services/job-queue.service';
|
||||
import { TorrentResult } from '@/lib/utils/ranking-algorithm';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.SelectTorrent');
|
||||
|
||||
/**
|
||||
* POST /api/requests/[id]/select-torrent
|
||||
@@ -59,7 +62,7 @@ export async function POST(
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`[SelectTorrent] User selected torrent: ${torrent.title} for request ${id}`);
|
||||
logger.info(`User selected torrent: ${torrent.title}`, { requestId: id });
|
||||
|
||||
// Trigger download job with the selected torrent
|
||||
const jobQueue = getJobQueueService();
|
||||
@@ -93,7 +96,7 @@ export async function POST(
|
||||
message: 'Torrent download initiated',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to select torrent:', error);
|
||||
logger.error('Failed to select torrent', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'DownloadError',
|
||||
|
||||
Reference in New Issue
Block a user