Search by

erloom / logger

yosefraim

Create Log and Middleware for routes.

2.0.0 2026-10-26 14:22 UTC

This package is auto-updated.

Last update: 2026-10-26 07:23:12 UTC


README

This package used with Filebeat on your server to generate log file within Laravel 9.x to 13.x (PHP 8.2+). Logging file later will be consumed to Logstash for logging purpose.

The feature of this package :

  • Convert Laravel Log into JSON formatting using channel dailybeat
  • Create Laravel Endpoint Log into JSON formatting using channel monitor
  • Create new Middleware alias for channel monitor to implement on your REST API, with a Middleware alias named request.log

Installation

  • Install with composer require erloom/logger, later you can use the 2 features available separately based on the installation instructions.

Change Usual log into JSON + Daily format

  • To change your usual logging into JSON, change your LOG_CHANNEL into dailybeat
    LOG_CHANNEL=dailybeat
    ...
    
  • Later, your logs will be generated at storage/logs with this format :
    
    
  • laravel-2021-03-04.log
  • laravel-2021-03-05.log ...
    with this content :
    

    {"@timestamp":"2021-03-04T11:20:36.053490+00:00","@version":1,"host":"xxx.local","message":"Test","type":"Laravel","channel":"local","level":"ERROR","monolog_level":400}

    
    

Log your REST API

  • To log your endpoint, the package automatically register middleware called request.log in your application. You can use it within your API routes.

    Route::get('/test', function (Request $request) {
      return 'Test';
    })->middleware('request.log');
    
  • Later, your logs will be generated at storage/logs with this format :

    
    
  • monitor-2021-03-04.log
  • monitor-2021-03-05.log ...
    with this content :
    

    {"@timestamp":"2021-10-04T09:37:09.348133+00:00","@version":1,"host":"xxx.local","message":"request_monitor","type":"Laravel","channel":"local","level":"INFO","monolog_level":200,"context":{"url":"YOUR_API_ROUTE","controller":null,"action":"Closure","request_method":"GET","ip_address":"REQUESTER_IP","user_agent":"USER_AGENT","request_body":"","response_status":200,"response_body":"[non-json body, 4 bytes]","duration":18.95594596862793}}

    
    

Redaction in the monitor log

The middleware never changes the request or response your app sees; only the logged copy is redacted.

  • The values of these keys are replaced with *** in request_body, response_body and the query string of url (at any nesting depth; if the value is an object/array, every value inside it is masked too): password, password_confirmation, current_password, re_password, token_pin, secret_token_pin, pin, old_pin, new_pin, re_new_pin, otp, token_otp, verification_code, refresh_token, access_token, client_secret, secret, token, authorization, api_key, x_api_key
  • Keys are compared after normalising (lowercase, _, -, . and spaces removed), so accessToken, Access-Token, access.token and ACCESS_TOKEN all match access_token. Only exact matches after normalising count, never substrings, so fields like bank_code, token_type or pin_code stay visible.
  • A JSON body that cannot be re-encoded (e.g. a number too large for a float) is logged as [unencodable json body].
  • request_body is redacted when it is JSON or application/x-www-form-urlencoded; response_body when it is JSON.
  • Any other non-empty body (HTML, plain text, binary, invalid JSON) is never logged raw: it is logged as [non-json body, N bytes]. An empty body stays "".
  • duration uses LARAVEL_START when defined, otherwise the request's REQUEST_TIME_FLOAT.

Requirements

PHP ^8.2, Laravel (illuminate/*) ^9.0|^10.0|^11.0|^12.0|^13.0.

Testing

composer install
vendor/bin/phpunit