NestJS interceptor to log HTTP requests and responses with full control, correlation IDs, file rotation, sensitive data masking, and production-ready features for traceability, compliance, and debugging.
- ✅ Log levels (
log
,warn
,error
,debug
) - ✅ JSON or plain-text format
- ✅ Sensitive field masking (e.g.
password
,token
,ssn
) - ✅ Correlation ID (auto-generated UUID or incoming
x-correlation-id
header) - ✅ File size-based log rotation (default 10MB, configurable)
- ✅ Automatically deletes logs older than 30 days
- ✅ Configurable output folder and filename
npm install logceptor
Add the following to your NestJS main.ts
:
// ✅ Public usage (npm installed)
import { LoggingInterceptor } from 'logceptor';
// 🔄 Optional: if you're testing a local version before publishing
// import { LoggingInterceptor } from '@mobilefirst/logger';
app.useGlobalInterceptors(new LoggingInterceptor({
format: 'json', // Log format: 'json' or 'text'
level: 'debug', // Log level: 'log' | 'debug' | 'warn' | 'error'
folder: 'logs', // Output directory for logs
filename: 'access.log', // File name
maskFields: ['password', 'token', 'ssn'], // Fields to mask
maxFileSizeMB: 10 // Max file size before rotation (default: 10)
}));
{"type":"request","timestamp":"2025-06-24T12:00:00Z","correlationId":"a3009ffe-430f-4481-ba55-eec7e6be9b44","method":"POST","url":"/api/users/login","body":{"email":"a@b.com","password":"****"}}
{"type":"response","timestamp":"2025-06-24T12:00:00Z","correlationId":"a3009ffe-430f-4481-ba55-eec7e6be9b44","url":"/api/users/login","duration":"31ms","response":{"message":"Logged in"}}
- Logs are saved in the defined folder (e.g.
logs/
) - Files older than 30 days are automatically deleted
Created by Mehul Jadav — MobileFirst
MIT © Mobilefirst Applications