Implement structured logging with pino (P2.17)
Add pino-based logger module for production observability: - JSON output to stdout for log aggregators (Loki, ELK) - Configurable via LOG_LEVEL environment variable (defaults to "info") - Log levels: error, warn, info, debug - Error objects serialized with type, message, and stack trace - Child logger support for bound context - ISO 8601 timestamps in all log entries Test coverage: 16 tests covering JSON format, log levels, error serialization, and child loggers. Total tests now: 553 passing across 31 test files. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
26
src/lib/logger.ts
Normal file
26
src/lib/logger.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// ABOUTME: Structured logging module using pino for JSON output to stdout.
|
||||
// ABOUTME: Configurable via LOG_LEVEL env var, outputs parseable logs for aggregators.
|
||||
|
||||
import pino from "pino";
|
||||
|
||||
/**
|
||||
* PhaseFlow logger configured per observability spec.
|
||||
*
|
||||
* Log levels: error, warn, info, debug
|
||||
* Output: JSON to stdout
|
||||
* Configuration: LOG_LEVEL env var (defaults to "info")
|
||||
*
|
||||
* Usage:
|
||||
* logger.info({ userId: "123" }, "User logged in");
|
||||
* logger.error({ err: error, userId: "123" }, "Operation failed");
|
||||
*/
|
||||
export const logger = pino({
|
||||
level: process.env.LOG_LEVEL || "info",
|
||||
formatters: {
|
||||
level: (label) => ({ level: label }),
|
||||
},
|
||||
timestamp: () => `,"timestamp":"${new Date().toISOString()}"`,
|
||||
messageKey: "message",
|
||||
});
|
||||
|
||||
export type Logger = typeof logger;
|
||||
Reference in New Issue
Block a user