Skip to content

feat(server): structured logging Phase 2 — migrate remaining console.* calls to the logger #303

Closed

Description

@gkhngyk

Summary

Follow-up to #245. Phase 1 (core logging infrastructure) shipped in #273 — server/src/lib/logger.js (pino + LOG_LEVEL + header redaction), server/src/middleware/request-id.js (per-request req.id + req.log child logger), and integration in server.js.

This issue tracks Phase 2: migrating the remaining console.* calls to the structured logger so production logs are leveled, JSON-formatted, and correlatable by request ID.

A sweep finds ~123 console.* calls across 23 files still unmigrated.

Guidance

  • Inside request handlers / middleware: use req.log.* so lines carry the request's requestId.
  • Everywhere else (workers, lib, scripts, config): import the module-level logger from lib/logger.js.
  • Level mapping: console.error → logger.error, console.warn → logger.warn, and console.log → logger.info or logger.debug depending on whether the line is operationally useful in production or just debug chatter.
  • Pass structured fields, not interpolated strings, where it helps querying — e.g. logger.error({ err, brandId }, 'tracking failed').
  • Operator-facing CLI scripts under server/src/scripts/ (e.g. backfill-shopping-cards.js) may keep console.* — they're meant for direct terminal output, same exception as the (now-removed) smoke scripts in feat(server): introduce structured logging (levels, request IDs, JSON) #245.

Do it incrementally

Per #245's guidance, do not land this as one 123-call rewrite. Migrate file-by-file or by area in small, reviewable PRs. Each PR can reference this issue.

Remaining files

Workers

  • workers/tracking-worker.js (20)
  • workers/content-worker.js (1)

Routes

  • routes/content.js (10)
  • routes/audits.js (8)
  • routes/tracking.js (7)
  • routes/prompts.js (6)
  • routes/volumes.js (4)
  • routes/traffic.js (1)
  • routes/topics.js (1)
  • routes/competitors.js (1)

Lib

  • lib/job-manager.js (10)
  • lib/cloro-scraper.js (10)
  • lib/job-runner.js (6)
  • lib/opportunity-generator.js (2)
  • lib/cloro-result-handler.js (2)
  • lib/shopping-cards.js (1)
  • lib/openai-tracker.js (1)
  • lib/audit/retry.js (1)
  • lib/audit/recommendations.js (1)
  • lib/audit/llm-signals.js (1)

Middleware / config / server

  • server.js (19 — the calls not converted in Phase 1)
  • middleware/index.js (4)
  • config/supabase.js (1)

Operator scripts (may stay console.*)

  • scripts/backfill-shopping-cards.js (5)

Done when

All non-script console.* calls are routed through the structured logger at appropriate levels, request-scoped lines use req.log, and grep -rc "console\." server/src --include="*.js" (excluding tests and scripts/) returns no matches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions