mesa/logmiddleware

Laravel audit trail middleware package

dev-main 2025-07-03 07:59 UTC

This package is auto-updated.

Last update: 2025-07-03 07:59:21 UTC


README

Custom Laravel middleware package to log request and response data in structured JSON format with rotating log files.

✨ Features

  • Logs request metadata: IP, method, URL, user agent, session, request body, response, duration, etc.
  • Outputs one compact JSON object per request
  • Log rotation based on a time interval (e.g. every 5 minutes)
  • Supports custom request ID for tracing

⚡ Installation

composer require mesa/logmiddleware

⚙ Configuration

1. Publish Configuration (Optional)

php artisan vendor:publish --tag=config

This will create config/auditlog.php:

return [
    'base_dir' => env('AUDITLOG_BASE_DIR', storage_path('logs/audit')),
    'rotate_interval' => env('AUDITLOG_ROTATE_INTERVAL', 300),
    'level' => env('AUDITLOG_LEVEL', 'debug'),
    'enable' => env('AUDITLOG_ENABLE', true)
];

2. Set Environment Variables

In .env file:

AUDITLOG_BASE_DIR=/absolute/path/to/storage/logs/audit
AUDITLOG_ROTATE_INTERVAL=300
AUDITLOG_LEVEL=debug
AUDITLOG_ENABLE=true

3. Register Logging Channel

Add to config/logging.php under channels:

'auditlog' => [
    'driver' => 'monolog',
    'handler' => \Mesa\LogMiddleware\Logging\LogRotatingFileHandler::class,
    'with' => [
        'baseDir' => config('auditlog.base_dir'),
        'level' => \Monolog\Logger::toMonologLevel(config('auditlog.level')),
        'interval' => config('auditlog.rotate_interval'),
        'enable' => config('auditlog.enable')
    ],
    'formatter' => \Monolog\Formatter\LineFormatter::class,
    'formatter_with' => [
        'format' => "%message%\n",
        'allowInlineLineBreaks' => false,
        'ignoreEmptyContextAndExtra' => true,
    ],
],

🛠 Middleware Setup

1. Create Request ID Middleware (Optional)

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Str;

class RequestIdMiddleware
{
    public function handle($request, Closure $next)
    {
        $requestId = $request->header('X-Request-ID') ?? (string) Str::uuid();

        $request->attributes->set('request_id', $requestId);

        $response = $next($request);
        $response->headers->set('X-Request-ID', $requestId);

        return $response;
    }
}

2. Add Middleware to Kernel

In app/Http/Kernel.php:

protected $middleware = [
    // ...
    \App\Http\Middleware\RequestIdMiddleware::class,
    \Mesa\LogMiddleware\Middleware\AuditLogMiddleware::class,
];

Note: Order matters. Request ID middleware must run before audit log middleware.

📊 Log Output

Log files will be stored at:

/storage/logs/audit/YYYYMMDD_HHMM.log

Each line is a compressed JSON object representing a request.

Example:

{"ip":"127.0.0.1","method":"GET","url":"https://yourapp.test/api","user_agent":"Mozilla/...","duration":142.67,"body":{"key":"value"},"response_status":200,"ts":"2025-07-03T04:00:00Z"}

✅ You're all set!

The middleware will now log every incoming request with full structured data into timestamped rotating log files.

Need to filter or extend logs? Just update AuditLogMiddleware.

MIT License | Maintained by @mesa