# Implementation Plan - Full Logging Implementation

## Phase 1: Infrastructure & Schema [checkpoint: 30f45d3]
- [x] Task: Database Migration for `system_logs` table (29dd7ef)
    - [x] Create SQL migration for `system_logs` table (id, level, time, msg, pid, hostname, requestId, userId, payload)
    - [x] Apply migration to local development database
- [x] Task: Dependency Management (069f069)
    - [x] Install `pino` and `pino-postgres` via pnpm
    - [x] Ensure types for `pino` are available
- [x] Task: Conductor - User Manual Verification 'Infrastructure & Schema' (Protocol in workflow.md)

## Phase 2: Core Logger & Context [checkpoint: ee45d74]
- [x] Task: AsyncLocalStorage Context Wrapper (TDD) (b659cc8)
    - [x] Write failing unit tests for `AsyncLocalStorage` context propagation
    - [x] Implement `LoggerContext` utility to manage `requestId` and `userId`
- [x] Task: Core Pino Logger with Redaction (TDD) (c413d0c)
    - [x] Write failing unit tests for the **Redaction Fail-safe** (verify `password` is redacted)
    - [x] Implement `logger.ts` singleton using `pino` with global redaction rules
    - [x] Verify unit tests pass for log levels (WARN, ERROR) and redaction
- [x] Task: Conductor - User Manual Verification 'Core Logger & Context' (Protocol in workflow.md)

## Phase 3: Middleware & Persistence Integration [checkpoint: 4883bed]
- [x] Task: Request Correlation Middleware (TDD) (37cb128)
    - [x] Write failing integration tests for `requestId` generation and auto-injection in logs
    - [x] Implement Next.js middleware to generate `requestId` and wrap execution in `AsyncLocalStorage`
- [x] Task: Database Transport via `pino-postgres` (TDD) (8733509)
    - [x] Write failing tests for log persistence to the database
    - [x] Configure and initialize the `pino-postgres` transport in the production/development logger setup
    - [x] Verify logs with `requestId` and `userId` are correctly stored in `system_logs`
- [x] Task: Conductor - User Manual Verification 'Middleware & Persistence Integration' (Protocol in workflow.md)

## Phase 4: Final Validation & Polish [checkpoint: 889a87f]
- [x] Task: End-to-End Audit (c62862e)
    - [x] Audit critical Server Actions (Orders, Auth) to ensure they are logging errors with `userId` and `requestId`
    - [x] Verify no PII (emails, names) is being leaked in logs
- [x] Task: Final Quality Gate Check (f50345b)
    - [x] Run full test suite (`pnpm test`)
    - [x] Verify code coverage for new modules (>80%)
- [x] Task: Conductor - User Manual Verification 'Final Validation & Polish' (Protocol in workflow.md)
