Skip to content

anibalalpizar/audit-log-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 Audit Log Middleware

A powerful middleware for logging user activities in your Express applications.

✨ Features

  • πŸ“ Automatically captures relevant data:
    • User information
    • Endpoint accessed
    • HTTP method
    • Date and time
    • Request body details
  • πŸ”§ Configurable detail levels (basic, intermediate, complete)
  • πŸ’Ύ Export options:
    • JSON files
    • CSV files
    • External services (Elasticsearch)

🎯 Use Cases

  • πŸ“‹ Applications requiring audit trails (e.g., GDPR or HIPAA compliance)
  • πŸ“Š Internal application monitoring
  • πŸ” User activity tracking
  • πŸ›‘οΈ Security logging

πŸš€ Installation

npm install audit-log-middleware

πŸ“– Basic Usage

const express = require("express");
const { createAuditMiddleware } = require("audit-log-middleware");

const app = express();

// Basic audit middleware configuration
const auditMiddleware = createAuditMiddleware({
  level: "complete",
  storage: {
    type: "file",
    options: {
      filename: "audit-logs.json",
    },
  },
});

// Apply middleware globally
app.use(auditMiddleware);

πŸ“ Example Output

[
  {
    "timestamp": "2024-01-01T12:00:00.000Z",
    "userId": "user123",
    "method": "GET",
    "endpoint": "/users/1",
    "userAgent": "Mozilla/5.0 (compatible; ExampleBrowser/1.0)",
    "ip": "71.198.38.7",
    "responseStatus": 200
  },
  {
    "timestamp": "2024-01-01T12:01:00.000Z",
    "userId": "user123",
    "method": "GET",
    "endpoint": "/",
    "userAgent": "Mozilla/5.0 (compatible; ExampleBrowser/1.0)",
    "ip": "216.73.163.219",
    "responseStatus": 200,
    "responseBody": "{\"message\":\"Hello World!\"}"
  }
]

βš™οΈ Configuration Options

Detail Levels

  • basic: Logs only essential information
  • intermediate: Adds request headers and response status
  • complete: Includes full request and response details

Storage Options

// File storage
{
  type: 'file',
  options: {
    filename: 'audit-logs.json'
  }
}
// Elasticsearch storage
{
  type: 'elasticsearch',
  options: {
    node: 'http://localhost:9200',
    index: 'audit-logs'
  }
}

πŸ”’ Security Considerations

  • Sensitive data can be filtered out using the exclude option
  • Supports data encryption for stored logs
  • Compliant with common security standards

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

Releases

No releases published

Packages

No packages published