Skip to content

refactor(logging): use structured error logs in server paths - #422

Merged
omegascorp merged 4 commits into
modelence:mainfrom
NilotpalK:refactor/structured-error-logging-294
Oct 28, 2026
Merged

omegascorp merged 4 commits into
modelence:mainfrom
NilotpalK:refactor/structured-error-logging-294

Conversation

@NilotpalK

@NilotpalK NilotpalK commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Production server and infrastructure errors bypass logError, so they do not consistently use the configured log level or structured context fields.

Route these errors through the existing helper across application startup, methods, routes, cron jobs, config sync, MongoDB connections, migrations, and Socket.IO. Preserve the original error in error and include relevant context such as methodName, path, alias, and collection. Existing APM capture calls, HTTP responses, and failure handling stay unchanged. The two process crash handlers keep direct console.error output so an unrecognized log level cannot hide crash diagnostics.

Refs #294. This follows the small call-site cleanup requested in #295 (comment). Auth logging is covered separately by #388; developer-facing takeover and detachment messages retain their existing output.

The documentation workflow also type-checks the existing Resend test, which imports Vitest. Add the missing dev dependency, update its lockfile, and expose npm test so a clean install can run the test and generate documentation.

Validation:

  • Clean Resend install, its attachment regression test, Resend type checking/build, full TypeDoc generation, unchanged API Reference navigation, and all 187 documentation content files' internal links pass.

  • All 106 tests in the server, route, and cron suites pass. The process-handler regression covers warn, none, and ERROR; method, route, and cron tests assert the structured log context.

  • Updated existing logging assertions and mocks for structured error context; the telemetry implementation is unchanged.

  • npm run lint:check, TypeScript type checking, npm run build, and importing the built server module with Node.js pass.

  • Prettier passes for all changed files. The full formatting check passes with --end-of-line auto; the default check flags existing CRLF files in this Windows checkout.

  • Full suite: 1,440 passed, 10 failed, identical to the baseline before these changes. Existing Windows failures are in authCache.test.ts (1), dev.test.ts (1), deploySpec.test.ts (1), and source.test.ts (7): file-mode and path-separator assumptions, symlink permissions, and an unavailable unzip command.


Note

Low Risk
Observability-only refactor with no change to HTTP responses or error handling semantics; unhandled process errors remain on console.error by design.

Overview
Replaces ad-hoc console.error calls across server and infrastructure paths with the telemetry logError helper so failures respect MODELENCE_LOG_LEVEL and emit a consistent shape: message plus a context object (typically { error }, with methodName, path, alias, or collection where relevant).

Coverage includes cloud backend connect failures, app startup (cron, background indexes, migrations), HTTP method and route handlers, config sync, MongoDB connect, cron job execution, Socket.IO, and related call sites. APM captureError, HTTP status/body behavior, and dev-only detachment/takeover messages are unchanged.

Unhandled rejections and uncaught exceptions still log via console.error (now with structured { error, promise } payloads) so crash diagnostics show up even when the log level is misconfigured; tests lock that in and confirm those paths do not call logError.

Tests and mocks were updated to expect the new structured logError signatures; the logError implementation itself is not modified in this PR.

Reviewed by Cursor Bugbot for commit 9e8082e. Bugbot is set up for automated code reviews on this repo. Configure here.

@NilotpalK
NilotpalK requested a review from omegascorp as a code owner October 19, 2026 20:10

@omegascorp omegascorp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for picking this up! It's the scoped cleanup we asked for in #295, and the diff is clean. One change before merging, plus a few minor notes.

Please change: keep crash output from being silenced

logError only prints when MODELENCE_LOG_LEVEL is debug, info or error. Any other value (warn, none, a typo like ERROR) now hides the uncaughtException and unhandledRejection output in app/server.ts. Before this PR, those always printed. Losing crash diagnostics because of a misconfigured env var is a bad trade.

Either option works:

  • keep raw console.error in those two process handlers, or
  • make logError print when it sees a level it doesn't recognise.

Minor, optional

  • Method, cron and route messages no longer include the name (Error calling ${methodName} → Error calling method: + { methodName }). The name is still in the output, so I'm fine with the structured version. Just be aware that searching logs for "Error calling todos.create" won't match any more.
  • server.test.ts, jobs.test.ts and handler.test.ts add logError: vi.fn() without asserting on it. A toHaveBeenCalledWith for the method, route and cron error paths would cover the new log shape, like you already did for db, migration and backendApi.

@NilotpalK

NilotpalK commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for catching that. Fixed in 9e8082e: the process handlers use console.error directly, and the method, route, and cron tests now check the structured log context.

I also fixed the docs failure in b797f0d. Resend now declares the Vitest dependency used by its existing test, with the lockfile updated and an npm test script. After syncing with main, a clean install, the Resend test, TypeScript, lint, build, and full docs checks pass locally. The full suite has 1,440 passes and the same 10 Windows failures. CI is now green: Build, Documentation, and Bugbot all passed.

@omegascorp omegascorp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for making the changes, looks good

@omegascorp
omegascorp merged commit bc0626e into modelence:main Oct 28, 2026
4 checks passed
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