Looking to hire Laravel developers? Try LaraJobs

laravel-logbarrel maintained by mxnwire

Description
Log every incoming HTTP request (and optionally its response) to a dedicated channel, with redaction and fine-grained options
Last update
2026/06/14 18:50 (dev-main)
License
Links
Downloads
1

Comments
comments powered by Disqus

Laravel LogBarrel

Log every incoming HTTP request — and optionally its response — to a dedicated channel, with sensitive-field redaction and fine-grained capture options. Drop it in and get a clean, queryable record of the traffic hitting your app.

Each request is written as a single log entry whose context holds the captured request and response data, for example:

{
    "method": "POST",
    "url": "https://app.test/login",
    "route": "login",
    "query": [],
    "body": { "email": "a@b.com", "password": "[REDACTED]" },
    "ip": "203.0.113.4",
    "user_agent": "Mozilla/5.0 ...",
    "user_id": 42,
    "user_email": "a@b.com",
    "status": 200,
    "duration_ms": 18.42
}

Installation

composer require mxnwire/laravel-logbarrel

The service provider is auto-discovered. By default the middleware is appended to the global HTTP stack, so every web/api request is logged with no further setup.

By default entries are written to a requests channel. Define it in config/logging.php (or set LOGBARREL_CHANNEL=stack/null to use your default):

'channels' => [
    'requests' => [
        'driver' => 'daily',
        'path' => storage_path('logs/requests.log'),
        'days' => 14,
    ],
],

Configuration

Publish the config to customise behaviour:

php artisan vendor:publish --tag=logbarrel-config

Key options in config/logbarrel.php:

  • enabled — master switch; when false the middleware passes requests through untouched (LOGBARREL_ENABLED).
  • register_global_middleware — when false, register manually with the logbarrel alias instead of the global stack.
  • channel / level / message — where, at what level and with what message each entry is logged. channel: null uses the default channel.
  • middleware_groups — only log requests whose matched route is in one of these groups (default web, api). Empty array logs everything, including unrouted requests.
  • except_paths — glob patterns (Str::is) whose paths are never logged (e.g. telescope*, _debugbar*).
  • request — toggle capture of method, url, route, query, body, headers, ip, user_agent, user.
  • response — toggle capture of status, duration, and (for JSON responses) content.
  • except_body / except_headers — keys/headers whose values are replaced with redacted (default [REDACTED]); body keys are matched recursively.
  • max_content_length — truncate captured bodies/response content beyond this many characters (null disables).
  • user_fields — fields captured for the authenticated user; each is a model attribute name or a callable receiving the user.

Per-request control

The LogBarrel facade lets you tailor — or switch off — logging for the current request from anywhere in the request lifecycle (a controller, form request, action, event listener…). Because the entry is written after the response is produced, anything you set is picked up when it matters. State is reset at the start of every request, so it never leaks to the next one.

use Mxnwire\LogBarrel\Facades\LogBarrel;

class CheckoutController
{
    public function store(Request $request)
    {
        // Don't log this request at all.
        LogBarrel::disable();

        // ...or force-log it even when globally off / outside the configured groups.
        LogBarrel::enable();

        // Attach extra context to the entry.
        LogBarrel::context(['order_id' => $order->id]);

        // Redact extra body keys on top of the configured list.
        LogBarrel::redact('ssn');                 // or ['ssn', 'card_number']

        // Capture only specific fields, or drop specific ones.
        LogBarrel::only('method', 'url', 'user'); // whitelist
        LogBarrel::without('body');               // blacklist

        // Send this request's entry to a different channel / message.
        LogBarrel::channel('audit')->message('checkout_completed');
    }
}

All methods are chainable and resolve the shared singleton, so LogBarrel::context([...])->redact('ssn')->channel('audit') works as expected.

Prefer a helper over the facade? logbarrel() returns the same singleton:

logbarrel()->disable();
logbarrel()->context(['order_id' => $order->id])->channel('audit');

Manual middleware registration

Set register_global_middleware to false, then apply the alias where needed:

Route::middleware('logbarrel')->group(function () {
    // ...
});

Testing

composer install
composer test

License

MIT.