Audit Log#
Request logging middleware with customizable storage. Track every API call with timing, user info, and optional body/query logging.
Basic Usage#
typescript
import { auditLog } from "@buntok/core/middlewares";
// Log all requests to console
app.use(auditLog());Info
Without a
storage option, logs go to logger.info (respects your logger config for format/file output). 5xx = error, 4xx = warn, others = log.Options#
| Option | Type | Default | Description |
|---|---|---|---|
| storage | (entry) => Promise<void> | logger.info | Custom storage function |
| excludePaths | string[] | [] | Paths to skip logging |
| excludeMethods | string[] | [] | HTTP methods to skip |
| logBody | boolean | false | Log request body |
| logQuery | boolean | true | Log query parameters |
| maxBodySize | number | 1024 | Max body size to log (bytes) |
Custom Storage#
typescript
app.use(auditLog({
storage: async (entry) => {
await db.auditLog.create({ data: entry });
},
excludePaths: ["/health", "/ping"],
excludeMethods: ["OPTIONS"],
logBody: true,
logQuery: true,
}));AuditLogEntry#
| Field | Type | Description |
|---|---|---|
| timestamp | string | ISO 8601 timestamp |
| method | string | HTTP method (GET, POST, etc.) |
| path | string | Request path |
| status | number | Response status code |
| duration | number | Request duration in ms |
| ip | string | Client IP address |
| userId | string | number | Authenticated user ID (if available) |
| userAgent | string | Client user agent |
| body | any | Request body (if logBody is true) |
| query | Record<string, any> | Query params (if logQuery is true) |
| error | string | Error message (if status >= 400) |
Database Schema (Prisma)#
typescript
// prisma/schema.prisma
model AuditLog {
id Int @id @default(autoincrement())
timestamp DateTime @default(now())
method String
path String
status Int
duration Float
ip String?
userId String?
userAgent String?
body Json?
query Json?
error String?
createdAt DateTime @default(now())
}