Skip to content

chore(server): structured logging Phase 2 — lib/config module-level logger (part 1) - #330

Merged
gkhngyk merged 1 commit into
mainfrom
chore/logging-phase2-lib
Jul 2, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
chore/logging-phase2-lib

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

First incremental slice of #303 (structured logging Phase 2). Migrates the console.* calls in the single-owner lib/ and config files to the module-level pino logger from lib/logger.js, using structured fields instead of interpolated strings.

None of these run inside request handlers, so they all use the module logger (the req.log request-scoped lines belong to the routes / middleware slices, which will follow as separate PRs per the issue's "do it incrementally" guidance).

Files migrated (~10 calls):

  • lib/audit/retry.js — retry warning → logger.warn({ label, attempt, attempts, delayMs, err }, …)
  • lib/audit/recommendations.js, lib/audit/llm-signals.js — failure → logger.error({ err, model }, …)
  • lib/opportunity-generator.js — 2× console.log → logger.info({ brandId, … }, …)
  • lib/cloro-result-handler.js — 2× console.error → logger.error({ err, … }, …)
  • lib/shopping-cards.js, lib/openai-tracker.js — console.error → logger.error({ err, … }, …)
  • config/supabase.js — missing-env startup fatal → logger.fatal(…) before process.exit(1)

Level mapping follows the issue: console.error → error, console.warn → warn, console.log → info. Behaviour is unchanged — lines are now leveled + JSON-formatted.

Related issue

Part of #303 (Phase 2 of #245). This is part 1; routes (req.log), workers, and server.js / middleware remain for follow-up PRs.

Type of change

  • chore — Maintenance

How to test

  1. yarn lint / yarn test from server/ pass (93 tests).
  2. grep -Rn "console\." server/src/lib/audit server/src/lib/opportunity-generator.js server/src/lib/cloro-result-handler.js server/src/lib/shopping-cards.js server/src/lib/openai-tracker.js server/src/config/supabase.js returns nothing.
  3. Triggering any of these paths (e.g. an audit LLM retry) now emits a structured JSON log line at the right level instead of a console.* string.

Checklist

…ogger (part 1)

First incremental slice of #303: migrate the console.* calls in the
single-owner lib/ and config files to the module-level pino logger, with
structured fields instead of interpolated strings.

- lib/audit/{retry,recommendations,llm-signals}.js
- lib/{opportunity-generator,cloro-result-handler,shopping-cards,openai-tracker}.js
- config/supabase.js (startup fatal -> logger.fatal before exit)

No request-scoped lines here (that's the routes/middleware slices); these
all use the module logger. Behavior unchanged; logs are now leveled + JSON.
@gkhngyk
gkhngyk merged commit d244a7f into main Jul 2, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the chore/logging-phase2-lib branch July 2, 2026 15:04
gkhngyk added a commit that referenced this pull request Jul 3, 2026
)

* chore(server): structured logging Phase 2 — routes slice (req.log)

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.

* fix(server): use module logger in traffic route, not req.log

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 added a commit that referenced this pull request Jul 4, 2026
Migrate the console.* calls in server/src/workers/ to the module-level
pino logger. Workers run outside the request lifecycle, so they use the
module logger (not req.log), with structured fields instead of
interpolated strings.

- tracking-worker.js (20): per-task progress chatter → logger.debug
  ({ taskId, scraperId }); operational milestones → logger.info
  ({ brandId, count, … }); failures → logger.error({ err, … });
  poll/stall retries → logger.warn. Customer prompt text is no longer
  written to logs — task/scraper ids identify the work instead.
- content-worker.js (1): generation summary → logger.info
  ({ brandId, generated })

Part of #303 (Phase 2 of #245), part 3 after #330 (lib/config) and the
routes slice. Level mapping per the issue; behaviour unchanged.
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.

1 participant