Add API request/response logging with correlation IDs#793
Open
Ajibose wants to merge 1 commit into
Open
Conversation
|
@Ajibose Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
Great job so far There’s just one blocker — the workflow is failing. Could you take a look and fix it so all checks pass? Happy to review again once that’s done. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #507
Summary
HttpLoggingInterceptor(NestJSAPP_INTERCEPTOR) that logs every HTTP request and response with a correlation ID, HTTP method/URL, status code, and response time in millisecondsAppLoggerServicebacked by winston with JSON structured output and optional daily log rotation (viawinston-daily-rotate-file) for ELK stack compatibilitymaskSensitiveData/maskHeadersutilities that redact fields likepassword,token,authorization,secret,credit_card,cvv,pin, andapi_keybefore writing to logsLoggingModuleimported inAppModule— no per-controller wiring neededCorrelation ID proof
Correlation IDs are generated and stored in
AsyncLocalStorageby the existingCorrelationIdMiddleware(readsX-Correlation-IDheader or auto-generates acid-*prefixed ID). The interceptor retrieves it viagetCorrelationId()and embeds it in every log line:{"timestamp":"2026-06-25T16:08:23.497Z","level":"info","service":"teachlink-backend","correlationId":"cid-1s4k2a-abc12345","event":"http_request","method":"POST","url":"/api/auth/login"}Request/response logging example
{"timestamp":"...","level":"info","correlationId":"cid-xxx","event":"http_request","method":"POST","url":"/api/courses","headers":{"content-type":"application/json","authorization":"***MASKED***"}} {"timestamp":"...","level":"info","correlationId":"cid-xxx","event":"http_response","method":"POST","url":"/api/courses","statusCode":201,"durationMs":47}Response time tracking example
durationMsis computed asDate.now() - startTimecaptured beforenext.handle()and emitted on both success and error paths via RxJStap:{"event":"http_response","statusCode":200,"durationMs":23}Sensitive data masking demonstration
Input body:
{ "email": "user@example.com", "password": "secret123" }Logged body:
{ "email": "user@example.com", "password": "***MASKED***" }Masked fields:
password,passwd,token,accesstoken,refresh_token,apikey,api_key,authorization,auth,key,secret,clientsecret,credit_card,cvv,cvc,pin,ssnUnit tests passing
Files changed
src/logging/sensitive-data.masker.tssrc/logging/logger.service.tssrc/logging/http-logging.interceptor.tssrc/logging/logging.module.tssrc/app.module.tsLoggingModulesrc/logging/*.spec.ts