feat(F-003): HTTP foundation with request context and error envelope

- request_id generated or sanitized-propagated on every request (x-request-id)
- structured JSON logging (pino), one correlated line per request, injectable logger
- error envelope v2 { error: { statusCode, code, message, details? }, requestId }
- 5xx messages always generic; stack traces stay in server logs only
- explicit parseJson (zod) input validation hook at the API layer
- README HTTP contract section; deps justified in spec/tech.md
- all gates approved; verify.sh green
This commit is contained in:
rikrdo
2026-08-14 22:13:28 +02:00
parent 425fedd13e
commit 41f144d7bd
26 changed files with 779 additions and 29 deletions

View File

@@ -0,0 +1,25 @@
/**
* Structured JSON logging. pino is the boring, fast, Fastify-ecosystem choice.
* No pretty printers, no transports: JSON lines to a writable destination.
*/
import pino, { type DestinationStream, type Logger } from 'pino';
const SERVICE_NAME = 'mercadodevida-backend';
export interface LoggerOptions {
/** Log level; defaults to LOG_LEVEL env var or 'info'. */
level?: string;
/** Explicit destination stream (tests use an in-memory stream). */
destination?: DestinationStream;
}
export function createLogger(options: LoggerOptions = {}): Logger {
const level = options.level ?? process.env.LOG_LEVEL ?? 'info';
const settings = { level, base: { service: SERVICE_NAME } };
if (options.destination) {
return pino(settings, options.destination);
}
return pino(settings);
}
export type { Logger };