Skip to content

chore(server): structured logging Phase 2 — routes slice (req.log) - #334

Merged
gkhngyk merged 2 commits into
mainfrom
feature/logging-phase2-routes
Jul 3, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
feature/logging-phase2-routes

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Second incremental slice of #303 (structured logging Phase 2), following #330. Migrates the console.* calls in server/src/routes/ to the structured pino logger.

Every migrated call lives inside a request handler, so it uses the request-scoped req.log child logger (which carries the per-request requestId from the request-id middleware) instead of raw console.error — with structured { err } fields rather than interpolated err.message strings.

Files migrated (~38 calls):

  • audits.js — 6 handlers → req.log.error({ err }, …); the 2 detached background functions (runAuditJob, createAndRunAudit, where req is out of scope) use the module-level logger + { auditId, brandId }
  • content.js (10), tracking.js (7), prompts.js (6), volumes.js (4), traffic.js (1), topics.js (1), competitors.js (1) — handler console.error → req.log.error({ err })

Level mapping unchanged (error → error); behaviour is identical — lines are now leveled + JSON-formatted + request-correlatable.

Related issue

Part of #303 (Phase 2 of #245). This is part 2 (routes / req.log). Workers, remaining lib/, and server.js / middleware follow as separate PRs per the issue's "do it incrementally" guidance.

Type of change

  • chore — Maintenance

How to test

  1. From server/: npm run lint, npm run format:check, and npm run test (93 tests) all pass.
  2. grep -rn "console\." server/src/routes/ returns nothing.
  3. Hitting any route error path (e.g. an invalid POST /api/audits) now emits a structured JSON log line at error level carrying requestId, instead of a console.error string.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • npm run lint passes (from server/)
  • npm run format:check passes (from server/)
  • Changes are focused — one concern per PR

Migrate the console.* calls in server/src/routes/ to the structured
logger. All lines run inside request handlers, so they use the
request-scoped req.log child (carries requestId) instead of raw
console.error, with { err } structured fields.

- audits.js — 6 handlers → req.log; 2 detached background functions
  (runAuditJob / createAndRunAudit) use the module logger + auditId/brandId
- content.js, prompts.js, tracking.js, volumes.js, traffic.js,
  competitors.js, topics.js — handler console.error → req.log.error({ err })

Part of #303 (Phase 2 of #245), part 2 of the incremental sweep after
#330. Level mapping unchanged (error → error); lines are now leveled +
JSON + request-correlatable.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the server’s structured logging Phase 2 migration by replacing console.error calls in route handlers with request-scoped structured logging via req.log, improving log level semantics, JSON structure, and request correlation.

Changes:

  • Migrates error logging in server/src/routes/* handlers from console.error(...) to req.log.error({ err }, '...').
  • Updates audits.js detached/background execution paths to use the module-level logger with useful identifiers (auditId, brandId) since req is not in scope.
  • Removes remaining console.* usage from the touched routes slice.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
server/src/routes/audits.js Migrates handler logs to req.log and uses module logger for background/detached audit functions.
server/src/routes/content.js Converts multiple handler console.error calls to structured req.log.error({ err }) logs.
server/src/routes/prompts.js Converts prompt suggestion route error logs to req.log.error({ err }).
server/src/routes/tracking.js Migrates tracking endpoints’ error logs to request-scoped structured logs.
server/src/routes/volumes.js Converts volume analysis/refresh/fetch error logs to req.log.error({ err }).
server/src/routes/topics.js Migrates topic suggestion error logging to req.log.
server/src/routes/competitors.js Migrates competitor suggestion error logging to req.log.
server/src/routes/traffic.js Migrates traffic track error log to req.log (but see review comment re: middleware ordering).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/src/routes/traffic.js Outdated
return res.status(204).end();
} catch (error) {
console.error('[traffic] Track error:', error.message);
req.log.error({ err: error }, 'traffic track error');
trafficRoutes is mounted before requestIdMiddleware in server.js (it
needs its own open CORS for navigator.sendBeacon from any origin), so
req.log is undefined on this public beacon path — req.log.error would
throw on the first error. Use the module-level logger instead.

Addresses the Copilot review on #334.
@gkhngyk
gkhngyk merged commit 0f4cb60 into main Jul 3, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the feature/logging-phase2-routes branch July 3, 2026 19:43
gkhngyk added a commit that referenced this pull request Jul 6, 2026
…e) (#351)

Migrate the last console.* calls (lib/cloro-scraper.js) to the module-level
pino logger. This file was deliberately held back from the earlier slices
to avoid churn with the query fan-out work (#341, now merged).

- submit/poll per-task chatter (task submitted, completed, polling) →
  logger.debug({ taskId, scraperId, ... }); operational start → logger.info
  ({ taskType, scraperId, region }); failures/timeouts → logger.error
  ({ ..., status/reason }). Customer prompt text is no longer written to the
  submit log — taskType/scraperId/region identify the task instead.

Closes #303: every non-script console.* in server/src now routes through the
structured logger. Phase 2 of #245 is complete (parts: #330, #334, #335,
#336, #337, and this).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants