feat: migrate backend logging from Winston to Pino with correlation IDs (refs #82)
All checks were successful
Deploy to Staging / Build Images (pull_request) Successful in 4m3s
Deploy to Staging / Deploy to Staging (pull_request) Successful in 32s
Deploy to Staging / Verify Staging (pull_request) Successful in 2m29s
Deploy to Staging / Notify Staging Ready (pull_request) Successful in 7s
Deploy to Staging / Notify Staging Failure (pull_request) Has been skipped
All checks were successful
Deploy to Staging / Build Images (pull_request) Successful in 4m3s
Deploy to Staging / Deploy to Staging (pull_request) Successful in 32s
Deploy to Staging / Verify Staging (pull_request) Successful in 2m29s
Deploy to Staging / Notify Staging Ready (pull_request) Successful in 7s
Deploy to Staging / Notify Staging Failure (pull_request) Has been skipped
- Replace Winston with Pino using API-compatible wrapper - Add LOG_LEVEL env var support with validation and fallback - Add correlation ID middleware (X-Request-Id from Traefik or UUID) - Configure PostgreSQL logging env vars (POSTGRES_LOG_STATEMENT, POSTGRES_LOG_MIN_DURATION) - Configure Redis loglevel via command args Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,24 +1,42 @@
|
||||
/**
|
||||
* @ai-summary Structured logging with Winston
|
||||
* @ai-context All features use this for consistent logging
|
||||
* @ai-summary Structured logging with Pino (Winston-compatible wrapper)
|
||||
* @ai-context All features use this for consistent logging. API maintains Winston compatibility.
|
||||
*/
|
||||
import * as winston from 'winston';
|
||||
import pino from 'pino';
|
||||
|
||||
export const logger = winston.createLogger({
|
||||
level: 'info',
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.json()
|
||||
),
|
||||
defaultMeta: {
|
||||
service: 'motovaultpro-backend',
|
||||
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
||||
const validLevels: LogLevel[] = ['debug', 'info', 'warn', 'error'];
|
||||
|
||||
const rawLevel = (process.env.LOG_LEVEL?.toLowerCase() || 'info') as LogLevel;
|
||||
const level = validLevels.includes(rawLevel) ? rawLevel : 'info';
|
||||
|
||||
if (process.env.LOG_LEVEL && rawLevel !== level) {
|
||||
console.warn(`Invalid LOG_LEVEL "${process.env.LOG_LEVEL}", falling back to "info"`);
|
||||
}
|
||||
|
||||
const pinoLogger = pino({
|
||||
level,
|
||||
formatters: {
|
||||
level: (label) => ({ level: label }),
|
||||
},
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
],
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
});
|
||||
|
||||
export default logger;
|
||||
// Wrapper maintains logger.info(msg, meta) API for backward compatibility
|
||||
export const logger = {
|
||||
info: (msg: string, meta?: object) => pinoLogger.info(meta || {}, msg),
|
||||
warn: (msg: string, meta?: object) => pinoLogger.warn(meta || {}, msg),
|
||||
error: (msg: string, meta?: object) => pinoLogger.error(meta || {}, msg),
|
||||
debug: (msg: string, meta?: object) => pinoLogger.debug(meta || {}, msg),
|
||||
child: (bindings: object) => {
|
||||
const childPino = pinoLogger.child(bindings);
|
||||
return {
|
||||
info: (msg: string, meta?: object) => childPino.info(meta || {}, msg),
|
||||
warn: (msg: string, meta?: object) => childPino.warn(meta || {}, msg),
|
||||
error: (msg: string, meta?: object) => childPino.error(meta || {}, msg),
|
||||
debug: (msg: string, meta?: object) => childPino.debug(meta || {}, msg),
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export default logger;
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
/**
|
||||
* @ai-summary Fastify request logging plugin
|
||||
* @ai-context Logs request/response details with timing
|
||||
* @ai-summary Fastify request logging plugin with correlation IDs
|
||||
* @ai-context Logs request/response details with timing and requestId
|
||||
*/
|
||||
import { FastifyPluginAsync } from 'fastify';
|
||||
import fp from 'fastify-plugin';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { logger } from '../logging/logger';
|
||||
|
||||
const loggingPlugin: FastifyPluginAsync = async (fastify) => {
|
||||
fastify.addHook('onRequest', async (request) => {
|
||||
request.startTime = Date.now();
|
||||
// Extract X-Request-Id from Traefik or generate new UUID
|
||||
request.requestId = (request.headers['x-request-id'] as string) || randomUUID();
|
||||
});
|
||||
|
||||
fastify.addHook('onResponse', async (request, reply) => {
|
||||
const duration = Date.now() - (request.startTime || Date.now());
|
||||
|
||||
|
||||
logger.info('Request processed', {
|
||||
requestId: request.requestId,
|
||||
method: request.method,
|
||||
path: request.url,
|
||||
status: reply.statusCode,
|
||||
@@ -24,13 +28,13 @@ const loggingPlugin: FastifyPluginAsync = async (fastify) => {
|
||||
});
|
||||
};
|
||||
|
||||
// Augment FastifyRequest to include startTime
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
startTime?: number;
|
||||
requestId?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export default fp(loggingPlugin, {
|
||||
name: 'logging-plugin'
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user