superapp
Advanced

Audit Logging

Track every query with user context, parameters, duration, and automatic retention policies.

Audit logging records every data request with the user, query, parameters, and execution time. Enable it for compliance, debugging, and security monitoring.

import { createEngine } from '@superapp/backend'

const engine = createEngine({
  connections: {
    main: { type: 'postgres', url: process.env.PG_URL! },
  },
  audit: {
    enabled: true,
  },
})

Full Configuration

audit: {
  enabled: true,        // Enable audit logging
  logQuery: true,       // Log the SQL query text
  logParams: true,      // Log query parameters
  logDuration: true,    // Log execution time in milliseconds
  retention: '90d',     // Auto-delete logs older than 90 days
}

Options Reference

OptionTypeDefaultDescription
enabledbooleanfalseEnable or disable audit logging
logQuerybooleantrueInclude the SQL query text in the log
logParamsbooleantrueInclude query parameters in the log
logDurationbooleantrueInclude execution duration in the log
retentionstring'90d'Auto-delete logs after this period (e.g., '30d', '1y')

What Gets Logged

Each audit log entry contains:

FieldDescription
timestampWhen the request was made
user_idAuthenticated user ID
tableTarget table (e.g., main.orders)
operationselect, insert, update, delete
queryGenerated SQL (if logQuery: true)
paramsQuery parameters (if logParams: true)
duration_msExecution time in milliseconds (if logDuration: true)
ip_addressClient IP address
roleUser's role at the time of the request
statussuccess or error

Retention

The retention option automatically purges old audit logs:

audit: {
  enabled: true,
  retention: '90d',  // Keep logs for 90 days
}

Supported formats:

  • '30d' — 30 days
  • '90d' — 90 days
  • '1y' — 1 year
  • '365d' — 365 days

PII Considerations

When logParams: true, query parameters are stored in plain text. If your queries include personally identifiable information (PII), consider:

// Option 1: Disable parameter logging
audit: {
  enabled: true,
  logParams: false,
}

// Option 2: Keep params but reduce retention
audit: {
  enabled: true,
  logParams: true,
  retention: '30d',
}

Querying Audit Logs

Audit logs are stored in the engine's internal database and accessible through the admin API:

# Get recent audit logs
curl -H "Authorization: Bearer $MASTER_KEY" \
  https://myapp.com/admin/api/audit?limit=100

# Filter by user
curl -H "Authorization: Bearer $MASTER_KEY" \
  https://myapp.com/admin/api/audit?user_id=usr_123

# Filter by table and operation
curl -H "Authorization: Bearer $MASTER_KEY" \
  https://myapp.com/admin/api/audit?table=main.orders&operation=delete

Disabling for Development

Turn off audit logging in development to reduce noise:

audit: {
  enabled: process.env.NODE_ENV === 'production',
}

On this page