feat(logging): add comprehensive logging to all API routes (Phase 4)

Applied withLogging() wrapper to all 24 API routes for consistent logging:

Process Control Routes:
- Start/stop/restart individual processes
- Start-all/stop-all/restart-all batch operations

Signal Routes:
- Signal individual processes
- Signal all processes
- Signal process groups

Group Management Routes:
- Start/stop/restart process groups
- Signal operations for groups

Configuration Routes:
- Get all configs (GET)
- Reload configuration (POST)
- Add/remove process groups (POST/DELETE)

Log Routes:
- Read main supervisord log
- Read process stdout/stderr logs
- Clear process logs (individual and all)

System Routes:
- Get system info
- Get all processes info
- Get individual process info
- Send stdin to process

All routes now include:
- Request/response logging with timing
- Automatic error handling and correlation IDs
- X-Request-ID header propagation
- Consistent metadata in responses

Also fixed Next.js 16 deprecation:
- Moved experimental.serverComponentsExternalPackages to serverExternalPackages

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-23 20:53:23 +01:00
parent b252a0b3bf
commit d592b58b75
26 changed files with 391 additions and 502 deletions

View File

@@ -1,22 +1,20 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
interface RouteParams {
params: Promise<{ name: string }>;
}
// DELETE - Clear process logs (both stdout and stderr)
export async function DELETE(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const client = createSupervisorClient();
const result = await client.clearProcessLogs(name);
return NextResponse.json({ success: result, message: `Logs cleared for ${name}` });
} catch (error: any) {
console.error('Supervisor clear process logs error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to clear process logs' },
{ status: 500 }
);
}
}
export const DELETE = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const client = createSupervisorClient();
const result = await client.clearProcessLogs(name);
return NextResponse.json({
success: result,
message: `Logs cleared for ${name}`,
processName: name,
});
}, 'clearProcessLogs');

View File

@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
export const dynamic = 'force-dynamic';
@@ -7,21 +8,14 @@ interface RouteParams {
params: Promise<{ name: string }>;
}
export async function GET(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const searchParams = request.nextUrl.searchParams;
const offset = parseInt(searchParams.get('offset') || '-4096', 10);
const length = parseInt(searchParams.get('length') || '4096', 10);
export const GET = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const searchParams = request.nextUrl.searchParams;
const offset = parseInt(searchParams.get('offset') || '-4096', 10);
const length = parseInt(searchParams.get('length') || '4096', 10);
const client = createSupervisorClient();
const logs = await client.tailProcessStderrLog(name, offset, length);
return NextResponse.json(logs);
} catch (error: any) {
console.error('Supervisor stderr log error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to fetch stderr logs' },
{ status: 500 }
);
}
}
const client = createSupervisorClient();
const logs = await client.tailProcessStderrLog(name, offset, length);
return NextResponse.json(logs);
}, 'readProcessStderr');

View File

@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
export const dynamic = 'force-dynamic';
@@ -7,21 +8,14 @@ interface RouteParams {
params: Promise<{ name: string }>;
}
export async function GET(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const searchParams = request.nextUrl.searchParams;
const offset = parseInt(searchParams.get('offset') || '-4096', 10);
const length = parseInt(searchParams.get('length') || '4096', 10);
export const GET = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const searchParams = request.nextUrl.searchParams;
const offset = parseInt(searchParams.get('offset') || '-4096', 10);
const length = parseInt(searchParams.get('length') || '4096', 10);
const client = createSupervisorClient();
const logs = await client.tailProcessStdoutLog(name, offset, length);
return NextResponse.json(logs);
} catch (error: any) {
console.error('Supervisor stdout log error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to fetch stdout logs' },
{ status: 500 }
);
}
}
const client = createSupervisorClient();
const logs = await client.tailProcessStdoutLog(name, offset, length);
return NextResponse.json(logs);
}, 'readProcessStdout');

View File

@@ -1,21 +1,20 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
interface RouteParams {
params: Promise<{ name: string }>;
}
export async function POST(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const client = createSupervisorClient();
const result = await client.restartProcess(name);
return NextResponse.json({ success: result, message: `Process ${name} restarted` });
} catch (error: any) {
console.error('Supervisor restart process error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to restart process' },
{ status: 500 }
);
}
}
export const POST = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const client = createSupervisorClient();
const result = await client.restartProcess(name);
return NextResponse.json({
success: result,
message: `Process ${name} restarted`,
processName: name,
});
}, 'restartProcess');

View File

@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
export const dynamic = 'force-dynamic';
@@ -7,17 +8,11 @@ interface RouteParams {
params: Promise<{ name: string }>;
}
export async function GET(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const client = createSupervisorClient();
const processInfo = await client.getProcessInfo(name);
return NextResponse.json(processInfo);
} catch (error: any) {
console.error('Supervisor process info error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to fetch process info' },
{ status: 500 }
);
}
}
export const GET = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const client = createSupervisorClient();
const processInfo = await client.getProcessInfo(name);
return NextResponse.json(processInfo);
}, 'getProcessInfo');

View File

@@ -1,36 +1,30 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
interface RouteParams {
params: Promise<{ name: string }>;
}
// POST - Send signal to a process
export async function POST(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const body = await request.json();
const { signal } = body;
export const POST = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const body = await request.json();
const { signal } = body;
if (!signal) {
return NextResponse.json(
{ error: 'Signal is required' },
{ status: 400 }
);
}
const client = createSupervisorClient();
const result = await client.signalProcess(name, signal);
return NextResponse.json({
success: result,
message: `Signal ${signal} sent to ${name}`,
});
} catch (error: any) {
console.error('Supervisor signal process error:', error);
if (!signal) {
return NextResponse.json(
{ error: error.message || 'Failed to send signal to process' },
{ status: 500 }
{ error: 'Signal is required' },
{ status: 400 }
);
}
}
const client = createSupervisorClient();
const result = await client.signalProcess(name, signal);
return NextResponse.json({
success: result,
message: `Signal ${signal} sent to ${name}`,
processName: name,
signal,
});
}, 'signalProcess');

View File

@@ -1,24 +1,23 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
interface RouteParams {
params: Promise<{ name: string }>;
}
export async function POST(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const body = await request.json().catch(() => ({}));
const wait = body.wait !== undefined ? body.wait : true;
export const POST = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const body = await request.json().catch(() => ({}));
const wait = body.wait !== undefined ? body.wait : true;
const client = createSupervisorClient();
const result = await client.startProcess(name, wait);
return NextResponse.json({ success: result, message: `Process ${name} started` });
} catch (error: any) {
console.error('Supervisor start process error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to start process' },
{ status: 500 }
);
}
}
const client = createSupervisorClient();
const result = await client.startProcess(name, wait);
return NextResponse.json({
success: result,
message: `Process ${name} started`,
processName: name,
wait,
});
}, 'startProcess');

View File

@@ -1,36 +1,29 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
interface RouteParams {
params: Promise<{ name: string }>;
}
// POST - Send input to process stdin
export async function POST(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const body = await request.json();
const { chars } = body;
export const POST = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const body = await request.json();
const { chars } = body;
if (chars === undefined || chars === null) {
return NextResponse.json(
{ error: 'Input characters are required' },
{ status: 400 }
);
}
const client = createSupervisorClient();
const result = await client.sendProcessStdin(name, chars);
return NextResponse.json({
success: result,
message: `Input sent to ${name}`,
});
} catch (error: any) {
console.error('Supervisor send stdin error:', error);
if (chars === undefined || chars === null) {
return NextResponse.json(
{ error: error.message || 'Failed to send input to process' },
{ status: 500 }
{ error: 'Input characters are required' },
{ status: 400 }
);
}
}
const client = createSupervisorClient();
const result = await client.sendProcessStdin(name, chars);
return NextResponse.json({
success: result,
message: `Input sent to ${name}`,
processName: name,
});
}, 'sendProcessStdin');

View File

@@ -1,24 +1,23 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
interface RouteParams {
params: Promise<{ name: string }>;
}
export async function POST(request: NextRequest, { params }: RouteParams) {
try {
const { name } = await params;
const body = await request.json().catch(() => ({}));
const wait = body.wait !== undefined ? body.wait : true;
export const POST = withLogging(async (request: NextRequest, { params }: RouteParams) => {
const { name } = await params;
const body = await request.json().catch(() => ({}));
const wait = body.wait !== undefined ? body.wait : true;
const client = createSupervisorClient();
const result = await client.stopProcess(name, wait);
return NextResponse.json({ success: result, message: `Process ${name} stopped` });
} catch (error: any) {
console.error('Supervisor stop process error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to stop process' },
{ status: 500 }
);
}
}
const client = createSupervisorClient();
const result = await client.stopProcess(name, wait);
return NextResponse.json({
success: result,
message: `Process ${name} stopped`,
processName: name,
wait,
});
}, 'stopProcess');

View File

@@ -1,21 +1,14 @@
import { NextResponse } from 'next/server';
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
// POST - Clear all process logs
export async function POST() {
try {
const client = createSupervisorClient();
const results = await client.clearAllProcessLogs();
return NextResponse.json({
success: true,
message: 'All process logs cleared',
results
});
} catch (error: any) {
console.error('Supervisor clear all logs error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to clear all logs' },
{ status: 500 }
);
}
}
export const POST = withLogging(async (request: NextRequest) => {
const client = createSupervisorClient();
const results = await client.clearAllProcessLogs();
return NextResponse.json({
success: true,
message: 'All process logs cleared',
results,
});
}, 'clearAllProcessLogs');

View File

@@ -1,30 +1,23 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
// POST - Restart all processes (stop then start)
export async function POST(request: NextRequest) {
try {
const body = await request.json().catch(() => ({}));
const wait = body.wait ?? true;
export const POST = withLogging(async (request: NextRequest) => {
const body = await request.json().catch(() => ({}));
const wait = body.wait ?? true;
const client = createSupervisorClient();
const client = createSupervisorClient();
// Stop all processes first
await client.stopAllProcesses(wait);
// Stop all processes first
await client.stopAllProcesses(wait);
// Then start them
const results = await client.startAllProcesses(wait);
// Then start them
const results = await client.startAllProcesses(wait);
return NextResponse.json({
success: true,
message: 'Restarted all processes',
results,
});
} catch (error: any) {
console.error('Supervisor restart all processes error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to restart all processes' },
{ status: 500 }
);
}
}
return NextResponse.json({
success: true,
message: 'Restarted all processes',
results,
wait,
});
}, 'restartAllProcesses');

View File

@@ -1,18 +1,12 @@
import { NextResponse } from 'next/server';
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
export const dynamic = 'force-dynamic';
export async function GET() {
try {
const client = createSupervisorClient();
const processes = await client.getAllProcessInfo();
return NextResponse.json(processes);
} catch (error: any) {
console.error('Supervisor processes error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to fetch processes' },
{ status: 500 }
);
}
}
export const GET = withLogging(async (request: NextRequest) => {
const client = createSupervisorClient();
const processes = await client.getAllProcessInfo();
return NextResponse.json(processes);
}, 'getAllProcessInfo');

View File

@@ -1,32 +1,25 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
// POST - Send signal to all processes
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { signal } = body;
export const POST = withLogging(async (request: NextRequest) => {
const body = await request.json();
const { signal } = body;
if (!signal) {
return NextResponse.json(
{ error: 'Signal is required' },
{ status: 400 }
);
}
const client = createSupervisorClient();
const results = await client.signalAllProcesses(signal);
return NextResponse.json({
success: true,
message: `Signal ${signal} sent to all processes`,
results,
});
} catch (error: any) {
console.error('Supervisor signal all processes error:', error);
if (!signal) {
return NextResponse.json(
{ error: error.message || 'Failed to send signal to all processes' },
{ status: 500 }
{ error: 'Signal is required' },
{ status: 400 }
);
}
}
const client = createSupervisorClient();
const results = await client.signalAllProcesses(signal);
return NextResponse.json({
success: true,
message: `Signal ${signal} sent to all processes`,
results,
signal,
});
}, 'signalAllProcesses');

View File

@@ -1,25 +1,18 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
// POST - Start all processes
export async function POST(request: NextRequest) {
try {
const body = await request.json().catch(() => ({}));
const wait = body.wait ?? true;
export const POST = withLogging(async (request: NextRequest) => {
const body = await request.json().catch(() => ({}));
const wait = body.wait ?? true;
const client = createSupervisorClient();
const results = await client.startAllProcesses(wait);
const client = createSupervisorClient();
const results = await client.startAllProcesses(wait);
return NextResponse.json({
success: true,
message: 'Started all processes',
results,
});
} catch (error: any) {
console.error('Supervisor start all processes error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to start all processes' },
{ status: 500 }
);
}
}
return NextResponse.json({
success: true,
message: 'Started all processes',
results,
wait,
});
}, 'startAllProcesses');

View File

@@ -1,25 +1,18 @@
import { NextRequest, NextResponse } from 'next/server';
import { createSupervisorClient } from '@/lib/supervisor/client';
import { withLogging } from '@/lib/utils/api-logger';
// POST - Stop all processes
export async function POST(request: NextRequest) {
try {
const body = await request.json().catch(() => ({}));
const wait = body.wait ?? true;
export const POST = withLogging(async (request: NextRequest) => {
const body = await request.json().catch(() => ({}));
const wait = body.wait ?? true;
const client = createSupervisorClient();
const results = await client.stopAllProcesses(wait);
const client = createSupervisorClient();
const results = await client.stopAllProcesses(wait);
return NextResponse.json({
success: true,
message: 'Stopped all processes',
results,
});
} catch (error: any) {
console.error('Supervisor stop all processes error:', error);
return NextResponse.json(
{ error: error.message || 'Failed to stop all processes' },
{ status: 500 }
);
}
}
return NextResponse.json({
success: true,
message: 'Stopped all processes',
results,
wait,
});
}, 'stopAllProcesses');