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#

OptionTypeDefaultDescription
storage(entry) => Promise<void>logger.infoCustom storage function
excludePathsstring[][]Paths to skip logging
excludeMethodsstring[][]HTTP methods to skip
logBodybooleanfalseLog request body
logQuerybooleantrueLog query parameters
maxBodySizenumber1024Max 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#

FieldTypeDescription
timestampstringISO 8601 timestamp
methodstringHTTP method (GET, POST, etc.)
pathstringRequest path
statusnumberResponse status code
durationnumberRequest duration in ms
ipstringClient IP address
userIdstring | numberAuthenticated user ID (if available)
userAgentstringClient user agent
bodyanyRequest body (if logBody is true)
queryRecord<string, any>Query params (if logQuery is true)
errorstringError 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())
}