Skip to content

chore: utilize AsyncContext to ensure logs have all request context#1824

Open
paustint wants to merge 1 commit into
mainfrom
chore/async-log-context
Open

chore: utilize AsyncContext to ensure logs have all request context#1824
paustint wants to merge 1 commit into
mainfrom
chore/async-log-context

Conversation

@paustint

Copy link
Copy Markdown
Contributor

Logs were sometimes hard to know which were associated to a give n transaction, this PR uses AsyncContext to store the logger so that the same logger instance with context is used across entire request lifecycle

This will make debugging errors and performing research much easier

Logs were sometimes hard to know which were associated to a give n transaction, this PR uses AsyncContext to store the logger so that the same logger instance with context is used across entire request lifecycle

This will make debugging errors and performing research much easier
Copilot AI review requested due to automatic review settings June 23, 2026 05:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 migrates request-scoped logging from req.log/res.log to an AsyncLocalStorage-backed request context so logs consistently include request identifiers and late-bound context (user/org/device) across the full request lifecycle.

Changes:

  • Add request-context (AsyncLocalStorage) with requestContextMiddleware, getLogger(), and enrichRequestContext().
  • Replace req.log/res.log usage across API and geo-ip-api with getLogger() and enrich context where discovered.
  • Update logging policy to emit 5xx access logs at error and adjust/add tests; add ESLint guard to prevent req.log/res.log regressions in apps/api.

Reviewed changes

Copilot reviewed 54 out of 54 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
libs/shared/node-utils/eslint.config.js Disable module-boundary rule in config
libs/salesforce-api/eslint.config.js Disable module-boundary rule in config
libs/email/eslint.config.js Disable module-boundary rule in config
libs/auth/server/src/lib/auth.service.ts Switch to getLogger(); enrich user/session
libs/auth/server/eslint.config.js Disable module-boundary rule in config
libs/api-types/src/lib/api-route.types.ts Remove log from API Request/Response types
libs/api-types/eslint.config.js Disable module-boundary rule in config
libs/api-config/src/lib/request-context.ts New AsyncLocalStorage request context + middleware
libs/api-config/src/lib/logging-policy.ts Log 5xx access lines at error
libs/api-config/src/lib/tests/request-context.spec.ts Add tests for request-context behavior
libs/api-config/src/lib/tests/logging-policy.spec.ts Update tests for 5xx access logging
libs/api-config/src/lib/tests/db-cache-provider.config.spec.ts Fix hoisted import ordering in test
libs/api-config/src/index.ts Export request-context; adjust init import order
libs/api-config/eslint.config.js Disable module-boundary rule in config
apps/jetstream-web-extension/eslint.config.js Disable module-boundary rule in config
apps/jetstream-web-extension-e2e/eslint.config.js Disable module-boundary rule in config
apps/jetstream-e2e/eslint.config.js Disable module-boundary rule in config
apps/geo-ip-api/src/route.utils.ts Use getLogger(); remove log types
apps/geo-ip-api/src/main.ts Mount request context middleware; use getLogger()
apps/geo-ip-api/eslint.config.js Disable module-boundary rule in config
apps/cron-tasks/src/tests/salesforce-org-expiration.spec.ts Fix hoisted import ordering in test
apps/cron-tasks/eslint.config.js Disable module-boundary rule in config
apps/api/src/main.ts Mount request context middleware after httpLogger
apps/api/src/app/utils/route.utils.ts Use request-scoped getLogger() in route wrapper
apps/api/src/app/utils/response.handlers.ts Replace res.log fallbacks with getLogger()
apps/api/src/app/utils/deferred-response.middleware.ts Replace res.log with getLogger() logging
apps/api/src/app/utils/tests/response.handlers.spec.ts Adapt tests to mocked getLogger()
apps/api/src/app/utils/tests/deferred-response.middleware.spec.ts Adapt tests to mocked getLogger()
apps/api/src/app/services/user-feedback.service.ts Use getLogger() for service logging
apps/api/src/app/services/external-auth.service.ts Use getLogger(); enrich user/device context
apps/api/src/app/services/tests/external-auth.service.spec.ts Mock getLogger()/enrichRequestContext()
apps/api/src/app/routes/team.routes.ts Replace res.log with getLogger()
apps/api/src/app/routes/route.middleware.ts Replace log usage; enrich user/org context
apps/api/src/app/routes/redirect.routes.ts Replace res.log with getLogger()
apps/api/src/app/routes/platform-event.routes.ts Replace log usage with getLogger()
apps/api/src/app/controllers/web-extension.controller.ts Replace res.log with getLogger()
apps/api/src/app/controllers/user.controller.ts Replace log usage with getLogger()
apps/api/src/app/controllers/user-feedback.controller.ts Replace log usage with getLogger()
apps/api/src/app/controllers/test.controller.ts Replace log usage with getLogger()
apps/api/src/app/controllers/team.controller.ts Replace log usage; update verification logging
apps/api/src/app/controllers/socket.controller.ts Use per-socket child logger; getLogger() for errors
apps/api/src/app/controllers/sf-bulk-api.controller.ts Replace res.log with getLogger()
apps/api/src/app/controllers/orgs.controller.ts Replace res.log with getLogger()
apps/api/src/app/controllers/oauth.controller.ts Replace log usage; pass getLogger() downstream
apps/api/src/app/controllers/desktop-app.controller.ts Replace res.log with getLogger()
apps/api/src/app/controllers/canvas.controller.ts Replace res.log with getLogger()
apps/api/src/app/controllers/billing.controller.ts Replace log usage with getLogger()
apps/api/src/app/controllers/auth.controller.ts Replace res.log with getLogger()
apps/api/src/app/controllers/tests/web-extension.controller.spec.ts Mock getLogger() for controller tests
apps/api/src/app/controllers/tests/user.controller.spec.ts Mock getLogger() for controller tests
apps/api/src/app/controllers/tests/team.controller.spec.ts Mock getLogger(); adapt domain verification tests
apps/api/src/app/controllers/tests/desktop-app.controller.spec.ts Mock getLogger() for controller tests
apps/api/src/app/controllers/tests/auth.controller.spec.ts Mock getLogger() for controller tests
apps/api/eslint.config.js Ban req.log/res.log (non-test) via restricted syntax

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

Comment on lines 1 to 4
// Sentry must initialize before any other module that should be auto-instrumented (db, http).
import './lib/api-sentry-config';
import './lib/api-db-config';
import './lib/api-sentry-config';
import './lib/env-config';
@@ -0,0 +1,100 @@
import { Maybe } from '@jetstream/types';
Comment on lines +11 to +13
// NOTE: `req.log` / `res.log` are intentionally NOT declared. Request-scoped logging now flows
// through `getLogger()` from `@jetstream/api-config` (AsyncLocalStorage). Reading `req.log` /
// `res.log` is disallowed — see the ESLint `no-restricted-syntax` guard in apps/api/.eslintrc.
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