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:
@@ -9,6 +9,9 @@ import bcrypt from 'bcrypt';
|
||||
import { generateAccessToken, generateRefreshToken } from '@/lib/utils/jwt';
|
||||
import { getEncryptionService } from '@/lib/services/encryption.service';
|
||||
import { getPlexService } from '@/lib/integrations/plex.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Setup.Complete');
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -172,12 +175,12 @@ export async function POST(request: NextRequest) {
|
||||
const serverInfo = await plexService.testConnection(plex.url, plex.token);
|
||||
if (serverInfo.success && serverInfo.info?.machineIdentifier) {
|
||||
machineIdentifier = serverInfo.info.machineIdentifier;
|
||||
console.log('[Setup] Fetched machineIdentifier:', machineIdentifier);
|
||||
logger.debug('Fetched machineIdentifier', { machineIdentifier });
|
||||
} else {
|
||||
console.warn('[Setup] Could not fetch machineIdentifier');
|
||||
logger.warn('Could not fetch machineIdentifier');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Setup] Error fetching machineIdentifier:', error);
|
||||
logger.error('Error fetching machineIdentifier', { error: error instanceof Error ? error.message : String(error) });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,7 +444,7 @@ export async function POST(request: NextRequest) {
|
||||
// BookDate configuration (optional, global for all users)
|
||||
// Note: libraryScope and customPrompt are now per-user settings, not required here
|
||||
if (bookdate && bookdate.provider && bookdate.apiKey && bookdate.model) {
|
||||
console.log('[Setup] Saving global BookDate configuration');
|
||||
logger.info('Saving global BookDate configuration');
|
||||
|
||||
const encryptionService = getEncryptionService();
|
||||
const encryptedApiKey = encryptionService.encrypt(bookdate.apiKey);
|
||||
@@ -478,9 +481,9 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[Setup] Global BookDate configuration saved');
|
||||
logger.debug('Global BookDate configuration saved');
|
||||
} else {
|
||||
console.log('[Setup] BookDate configuration skipped (missing provider, apiKey, or model)');
|
||||
logger.debug('BookDate configuration skipped (missing provider, apiKey, or model)');
|
||||
}
|
||||
|
||||
// Mark setup as complete
|
||||
@@ -502,9 +505,9 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
|
||||
console.log('[Setup] Auto jobs enabled');
|
||||
logger.debug('Auto jobs enabled');
|
||||
|
||||
console.log('[Setup] Configuration saved successfully');
|
||||
logger.info('Configuration saved successfully');
|
||||
|
||||
// Return response with tokens if admin user was created
|
||||
if (adminUser && accessToken && refreshToken) {
|
||||
@@ -530,7 +533,7 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Setup] Failed to save configuration:', error);
|
||||
logger.error('Failed to save configuration', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Setup.Status');
|
||||
|
||||
/**
|
||||
* GET /api/setup/status
|
||||
@@ -24,7 +27,7 @@ export async function GET(request: NextRequest) {
|
||||
});
|
||||
} catch (error) {
|
||||
// If database is not ready or table doesn't exist, setup is not complete
|
||||
console.error('[Setup Status] Check failed:', error);
|
||||
logger.error('Check failed', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json({
|
||||
setupComplete: false,
|
||||
});
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { QBittorrentService } from '@/lib/integrations/qbittorrent.service';
|
||||
import { SABnzbdService } from '@/lib/integrations/sabnzbd.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Setup.TestDownloadClient');
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -80,7 +83,7 @@ export async function POST(request: NextRequest) {
|
||||
{ status: 400 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('[Setup] Download client test failed:', error);
|
||||
logger.error('Download client test failed', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { Issuer } from 'openid-client';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Setup.TestOIDC');
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -65,7 +68,7 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Test OIDC] Discovery failed:', error);
|
||||
logger.error('Discovery failed', { error: error instanceof Error ? error.message : String(error) });
|
||||
|
||||
// Determine error message
|
||||
let errorMessage = 'OIDC discovery failed';
|
||||
|
||||
@@ -6,21 +6,24 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Setup.TestPaths');
|
||||
|
||||
async function testPath(dirPath: string): Promise<boolean> {
|
||||
try {
|
||||
// Try to access the path
|
||||
try {
|
||||
await fs.access(dirPath);
|
||||
console.log(`[Setup] Path exists: ${dirPath}`);
|
||||
logger.debug('Path exists', { path: dirPath });
|
||||
} catch (accessError) {
|
||||
// Path doesn't exist, try to create it
|
||||
console.log(`[Setup] Path doesn't exist, creating: ${dirPath}`);
|
||||
logger.debug('Path does not exist, creating', { path: dirPath });
|
||||
try {
|
||||
await fs.mkdir(dirPath, { recursive: true });
|
||||
console.log(`[Setup] Successfully created path: ${dirPath}`);
|
||||
logger.debug('Successfully created path', { path: dirPath });
|
||||
} catch (mkdirError) {
|
||||
console.error(`[Setup] Failed to create path ${dirPath}:`, mkdirError);
|
||||
logger.error('Failed to create path', { path: dirPath, error: mkdirError instanceof Error ? mkdirError.message : String(mkdirError) });
|
||||
// If mkdir fails, it means the parent mount doesn't exist or isn't writable
|
||||
return false;
|
||||
}
|
||||
@@ -35,7 +38,7 @@ async function testPath(dirPath: string): Promise<boolean> {
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`[Setup] Path test failed for ${dirPath}:`, error);
|
||||
logger.error('Path test failed', { path: dirPath, error: error instanceof Error ? error.message : String(error) });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -81,7 +84,7 @@ export async function POST(request: NextRequest) {
|
||||
message: 'Directories are ready and writable (created if needed)',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Setup] Path validation failed:', error);
|
||||
logger.error('Path validation failed', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getPlexService } from '@/lib/integrations/plex.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Setup.TestPlex');
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -49,7 +52,7 @@ export async function POST(request: NextRequest) {
|
||||
})),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Setup] Plex test failed:', error);
|
||||
logger.error('Plex test failed', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { ProwlarrService } from '@/lib/integrations/prowlarr.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Setup.TestProwlarr');
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -38,7 +41,7 @@ export async function POST(request: NextRequest) {
|
||||
})),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[Setup] Prowlarr test failed:', error);
|
||||
logger.error('Prowlarr test failed', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
|
||||
Reference in New Issue
Block a user