request-log-starter
Version:
Request logging middleware for Nuxt 3 applications
105 lines (80 loc) • 2.67 kB
Markdown
# Nuxt 3 Request Logger
A simple, configurable request logging middleware for Nuxt 3 applications.
## Features
- 📝 Logs HTTP requests to your Nuxt 3 application
- ⏱️ Records response times
- 🔒 Masks sensitive headers
- 🧩 Configurable output format (JSON or text)
- 🚫 Allows custom filtering of requests
## Installation
```bash
# npm
npm install @nuxt3-auth-example/request-log-starter
# yarn
yarn add @nuxt3-auth-example/request-log-starter
# pnpm
pnpm add @nuxt3-auth-example/request-log-starter
```
## Setup
Add the module to your `nuxt.config.ts`:
```typescript
export default defineNuxtConfig({
modules: ["@nuxt3-auth-example/request-log-starter"],
// Optional configuration
requestLog: {
enabled: true,
logBody: false,
logHeaders: true,
format: "json", // or 'text'
maskHeaders: ["authorization", "cookie", "x-api-key"],
},
});
```
## Configuration
| Option | Type | Default | Description |
| ------------- | ---------------- | ----------------------------- | --------------------------------- |
| `enabled` | boolean | `true` | Enable or disable request logging |
| `logBody` | boolean | `false` | Include request body in logs |
| `logHeaders` | boolean | `false` | Include HTTP headers in logs |
| `format` | 'json' \| 'text' | `'json'` | Output format |
| `maskHeaders` | string[] | `['authorization', 'cookie']` | Headers to mask for privacy |
| `filter` | function | `undefined` | Custom filter function |
### Custom Filter Function
You can provide a custom filter function to control which requests get logged:
```typescript
export default defineNuxtConfig({
modules: ["@nuxt3-auth-example/request-log-starter"],
requestLog: {
filter: (event) => {
// Skip logging for static assets
if (event.path.startsWith("/assets/") || event.path.startsWith("/_nuxt/")) {
return false;
}
return true;
},
},
});
```
## Log Output
### JSON Format (default)
```json
{
"timestamp": "2023-05-12T15:43:21.123Z",
"method": "POST",
"url": "https://example.com/api/users",
"statusCode": 201,
"responseTime": 42,
"ip": "127.0.0.1",
"userAgent": "Mozilla/5.0...",
"headers": {
"content-type": "application/json",
"authorization": "***MASKED***"
}
}
```
### Text Format
```
[2023-05-12T15:43:21.123Z] POST https://example.com/api/users 201 42ms
```
## License
MIT