apity
v1.0.12
Published
Simple request logging middleware for Express. Sends logs to Apity Router. Includes server metrics collection (CPU, Memory).
Downloads
1,309
Maintainers
Readme
apity
Simple request logging middleware for Express. Sends logs to Apity Router.
Installation
npm install apityQuick Start
import express from 'express';
import { apityLogger } from 'apity';
const app = express();
app.use(express.json());
app.use(apityLogger({ appId: 'your-app-uuid' }));
// Your routes...
app.get('/api/users', (req, res) => {
res.json({ users: [] });
});
app.listen(3000);Configuration
apityLogger({
// Required
appId: string, // UUID from Apity Router Master DB
// Optional
routerUrl?: string, // Router URL (default: https://router.apity.io)
ignorePaths?: string[], // Paths to skip (default: ['/health', '/', '/favicon.ico'])
sensitiveHeaders?: string[], // Additional headers to mask (default includes: authorization, cookie, x-api-key)
sensitiveFields?: string[], // Additional body fields to mask (default includes: password, token, secret)
silent?: boolean, // Suppress error logs (default: false)
debug?: boolean, // Enable debug logging (default: false)
enableMetrics?: boolean, // Enable server metrics collection (CPU, Memory) (default: false)
metricsInterval?: number // Metrics collection interval in seconds (default: 30)
});Server Metrics
Enable automatic collection of server metrics (CPU, Memory):
app.use(apityLogger({
appId: 'your-app-uuid',
enableMetrics: true, // Enable metrics collection
metricsInterval: 30, // Collect every 30 seconds
}));Metrics are automatically sent to the router and stored in the server_metrics table. See Client Metrics Setup for details.
What Gets Logged
Database (metadata)
- HTTP method, path, status code
- Duration (ms)
- User ID & username (if authenticated)
- Client IP, device type, OS, app version
- Query & path parameters
- Request/response sizes
- Error flag
S3 (full log)
- Complete request (headers, body)
- Complete response (headers, body)
- Truncated if too large (10KB request, 50KB response)
Debug Mode
Enable debug logging to see what's happening:
app.use(apityLogger({
appId: 'your-app-uuid',
debug: true
}));Output:
[apity] 🚀 Logger initialized
[apity] App ID: your-app-uuid
[apity] Router URL: https://router.apity.io
[apity] Ignore paths: /health, /, /favicon.ico
[apity] 📥 Incoming request: GET /api/users
[apity] 📤 Response sent: GET /api/users → 200 (45ms)
[apity] 📤 Sending log to https://router.apity.io/api/v1/logs
[apity] Method: GET /api/users
[apity] Status: 200, Duration: 45ms
[apity] ✅ Log sent successfully (120ms)Features
✅ Non-blocking - Logs are sent asynchronously after response
✅ Auto-masking - Sensitive data (passwords, tokens) automatically hidden
✅ User-Agent parsing - Extracts device type, OS, app version
✅ TypeScript - Full type definitions included
✅ Zero config - Works out of the box with sensible defaults
✅ Debug mode - Verbose logging for troubleshooting
Environment Variables
You can use environment variables for the app ID:
app.use(apityLogger({
appId: process.env.APITY_APP_ID!
}));# .env
APITY_APP_ID=your-uuidLicense
MIT
