Repository navigation
refactor(logging): use structured error logs in server paths - #422
Conversation
omegascorp
left a comment
There was a problem hiding this comment.
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.errorin those two process handlers, or - make
logErrorprint 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.tsandhandler.test.tsaddlogError: vi.fn()without asserting on it. AtoHaveBeenCalledWithfor the method, route and cron error paths would cover the new log shape, like you already did for db, migration and backendApi.
|
Thanks for catching that. Fixed in 9e8082e: the process handlers use 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 |
omegascorp
left a comment
There was a problem hiding this comment.
Thanks for making the changes, looks good
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
errorand include relevant context such asmethodName,path,alias, andcollection. Existing APM capture calls, HTTP responses, and failure handling stay unchanged. The two process crash handlers keep directconsole.erroroutput 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 testso 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, andERROR; 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), andsource.test.ts(7): file-mode and path-separator assumptions, symlink permissions, and an unavailableunzipcommand.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.errorcalls across server and infrastructure paths with the telemetrylogErrorhelper so failures respectMODELENCE_LOG_LEVELand emit a consistent shape: message plus a context object (typically{ error }, withmethodName,path,alias, orcollectionwhere 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 calllogError.Tests and mocks were updated to expect the new structured
logErrorsignatures; thelogErrorimplementation 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.