Whether to include the response body in the log output.Set to false to exclude response bodies from logs (useful for large payloads or reducing log size).
Whether to include query parameters in the log output.Set to false to exclude query parameters from logs (useful if query params contain sensitive data).
List of header names to exclude from logs. Matching is case-insensitive.Useful for excluding sensitive headers like authorization tokens, cookies, or API keys.
logger({ excludedHeaders: [ 'Authorization', // Case-insensitive 'COOKIE', 'X-API-KEY', 'X-Session-Token' ]})// These headers will not appear in the logs
Optional async function that takes an IP address string and returns an object containing IP-related information (e.g., geo-location).The function receives the client’s IP address and should return an IpInfo object with location data.
Optional callback that receives the log data object for custom processing.This function is called after log data is formatted but before it’s written to console. Use it to send logs to external services, databases, or analytics platforms.
logger({ onLog: async (logData) => { // Send to external service await analyticsService.track(logData); // Store in database await db.logs.insert(logData); // Send alerts for errors if (logData.error) { await alertingService.notify(logData); } }})
Show Error Handling in onLog
If the onLog callback throws an error, it will be caught and logged as a warning without breaking the request flow:
logger({ onLog: async (logData) => { throw new Error('External service unavailable'); // Error is caught and logged: // console.warn('onLog callback threw:', Error) // Request continues normally }})
List of field names to mask in request body, response body, headers, and query parameters.Masked fields will have their values replaced with "***MASKED***". Works with nested objects and arrays.
Custom function to determine the log level based on log data.Return 'info', 'warn', or 'error' to control which console method is used. If not provided, the default behavior is:
'error' if error is present
'warn' if status code >= 400
'info' otherwise
logger({ customLogLevel: (logData) => { // Log slow requests as warnings if (logData.timeTaken > 1000) return 'warn'; // Log errors if (logData.error) return 'error'; // Log client errors as warnings if (logData.statusCode >= 400 && logData.statusCode < 500) return 'warn'; // Log server errors if (logData.statusCode >= 500) return 'error'; // Everything else as info return 'info'; }})
Custom function to format or transform log data before logging.The function receives the complete LogData object and can return a modified version or a completely different structure.
Whether to automatically generate a request ID if one is not already present in the X-Request-ID header.When enabled, a UUID will be generated and added to response headers as X-Request-ID and included in log data.
logger({ autoGenerateRequestId: true})// Request ID is added to:// 1. Response header: X-Request-ID// 2. Log data: logData.requestId
Show Example: Using Request IDs
app.use(logger({ autoGenerateRequestId: true }));app.get('/api/users', (req, res) => { // Request ID is available in response headers const requestId = res.getHeader('X-Request-ID'); // Use it for distributed tracing await externalService.call({ requestId }); res.json({ users: [] });});
If a request already has an X-Request-ID header, it will be preserved and not overwritten.
Function to conditionally determine whether to log a request.Return true to log the request, false to skip logging. The function is called early in the middleware chain before any logging occurs.
logger({ shouldLog: (req, res) => { // Skip health checks if (req.url === '/health') return false; // Skip static assets if (req.url.startsWith('/static')) return false; // Skip successful GET requests in production if (process.env.NODE_ENV === 'production' && req.method === 'GET' && res.statusCode < 400) { return false; } // Log everything else return true; }})
Show Error Handling in shouldLog
If the shouldLog function throws an error, a warning is logged and the request is logged anyway:
logger({ shouldLog: (req, res) => { throw new Error('Decision logic failed'); // Warning logged: 'shouldLog function threw:', Error // Request continues and is logged }})