erloom / logger
Create Log and Middleware for routes.
Requires
- php: ^8.2
- illuminate/contracts: ^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/http: ^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/routing: ^9.0|^10.0|^11.0|^12.0|^13.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- phpunit/phpunit: ^11.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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
monitorto implement on your REST API, with a Middleware alias namedrequest.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_CHANNELintodailybeatLOG_CHANNEL=dailybeat ... - Later, your logs will be generated at
storage/logswith 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.login 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/logswith 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
***inrequest_body,response_bodyand the query string ofurl(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), soaccessToken,Access-Token,access.tokenandACCESS_TOKENall matchaccess_token. Only exact matches after normalising count, never substrings, so fields likebank_code,token_typeorpin_codestay 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_bodyis redacted when it is JSON orapplication/x-www-form-urlencoded;response_bodywhen 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"". durationusesLARAVEL_STARTwhen defined, otherwise the request'sREQUEST_TIME_FLOAT.
Requirements
PHP ^8.2, Laravel (illuminate/*) ^9.0|^10.0|^11.0|^12.0|^13.0.
Testing
composer install
vendor/bin/phpunit