Skip to content

chore(server): structured logging Phase 2 — server.js + middleware - #337

Merged
gkhngyk merged 1 commit into
mainfrom
feature/logging-phase2-server-mw
Jul 4, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feature/logging-phase2-server-mw

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Fifth slice of #303 (structured logging Phase 2). Migrates the remaining console.* calls in server.js and middleware/index.js to the pino logger (server.js already imported it in Phase 1).

Files migrated (~25 calls):

  • server.js (21):
    • request handlers (internal CRON_SECRET endpoints, /cloro/callback) → req.log.* with structured fields (taskId, scraperId, ip, status, …)
    • module-scope lines (runDailyTracking summary, server.listen, self-hosted cron trigger/schedule) → module logger
    • global error handler uses the module logger, not req.log — an error thrown before requestIdMiddleware (public traffic routes / body parser) has no req.log, so req.log.error there would itself throw
  • middleware/index.js (4): socket auth + domain-check paths → module logger.error({ err }) / logger.warn (sockets carry no requestId, and the HTTP domain-check variant isn't guaranteed to run after requestIdMiddleware)

Level mapping per the issue. Behaviour unchanged; lines are now leveled + JSON.

Related issue

Part of #303 (Phase 2 of #245) — the final slice, following #330, and the routes / workers / lib-jobs slices. The only file left after this is lib/cloro-scraper.js (10 calls), intentionally deferred to land together with the query fan-out work (#332) to avoid churn/conflicts. Once #332 lands, #303 can be closed.

Type of change

  • chore — Maintenance

How to test

  1. From server/: npm run lint, npm run format:check, npm run test (93 tests) all pass.
  2. grep -n "console\." server/src/server.js server/src/middleware/index.js returns nothing.
  3. Booting the server logs a structured server running line; a /cloro/callback delivery logs structured request-scoped lines (req.log) carrying taskId/scraperId; an unhandled error logs via the module logger with { err }.

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 remaining console.* calls in server.js and middleware/index.js
to the pino logger (server.js already imported it in Phase 1).

- server.js (21): request handlers (internal endpoints, /cloro/callback)
  → req.log with structured fields (taskId, scraperId, ip, …); cron /
  startup lines (runDailyTracking, listen, self-hosted cron) → module
  logger. The global error handler uses the module logger, not req.log,
  because an error thrown before requestIdMiddleware (public traffic
  routes / body parser) has no req.log.
- middleware/index.js (4): socket + domain-check paths → module logger
  ({ err }); no req.log since sockets carry no requestId and the HTTP
  variant isn't guaranteed to run after requestIdMiddleware.

Final slice of #303 (Phase 2 of #245) except lib/cloro-scraper.js, which
is deferred to land with the query fan-out work (#332). Behaviour
unchanged; lines are now leveled + JSON.

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 completes the structured-logging migration for the server entrypoint and core middleware by replacing remaining console.* calls with the pino logger, using request-scoped req.log inside handlers and the module logger elsewhere (including the global error handler).

Changes:

  • Replace remaining console.* calls in server/src/server.js with req.log.* (request handlers) and module logger.* (startup/cron/global error handler).
  • Replace remaining console.* calls in server/src/middleware/index.js with module logger.* for socket and domain-check paths.
  • Add structured fields to key log lines (e.g., taskId, scraperId, brandId, status, ip, reason).

Reviewed changes

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

File Description
server/src/server.js Migrates remaining logging in server handlers/startup/cron/error handling to structured pino logging (req.log + module logger).
server/src/middleware/index.js Replaces console logging in socket auth + domain checks with module-level structured pino logging.

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

Comment thread server/src/server.js
}

console.log(`[cron] Daily tracking: triggered=${triggered} total_brands=${brands.length}`);
logger.info({ triggered, total: brands.length }, 'daily tracking triggered');
@gkhngyk
gkhngyk merged commit 319187b into main Jul 4, 2026
6 checks passed
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