logceptor
Version:
NestJS interceptor to log HTTP requests and responses with full control, correlation IDs, file rotation, sensitive data masking, and production-ready features.
75 lines (49 loc) • 2.23 kB
Markdown
# logceptor
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.
## ✨ Features
- ✅ 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
## 📦 Installation
```bash
npm install logceptor
```
## 🚀 Usage (Global Interceptor)
Add the following to your NestJS `main.ts`:
```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)
}));
```
## 🧪 Output Example (`logs/access.log`)
```json
{"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"}}
```
## 🧹 Automatic Log Cleanup
- Logs are saved in the defined folder (e.g. `logs/`)
- Files older than **30 days** are automatically deleted
## 👤 Author
Created by **Mehul Jadav** — [MobileFirst](https://mobilefirst.in)
## 📝 License
MIT © Mobilefirst Applications