chelout / laravel-http-logger
A Laravel package to log HTTP requests, headers and sessions
Installs: 2 027
Dependents: 0
Suggesters: 0
Security: 0
Stars: 25
Watchers: 4
Forks: 3
Open Issues: 0
Requires
- php: ^7.1.3
- illuminate/support: 5.8.*
- monolog/monolog: ~1.12
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-10-29 05:50:43 UTC
README
This package provides a middleware to log incoming http requests data (body data, files, headers and session data). It utilizes Laravel 5.6 logging servises functionality. This package might be useful to log user requests to public apis.
Installation
You can install the package via composer:
composer require chelout/laravel-http-logger
Optionally you can publish the configfile with:
php artisan vendor:publish --provider="Chelout\HttpLogger\HttpLoggerServiceProvider" --tag="config"
This is the contents of the published config file:
return [ /* * Log file path */ 'path' => storage_path('logs/http.log'), /* * The maximal amount of files to keep (0 means unlimited) */ 'max_files' => 5, /* * Log methods * [] - log all methods * ['get','post'] - log only 'get' and 'post' methods */ 'methods' => [], /* * Log message format. * For for details see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format * and https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php */ 'format' => "[%datetime%] %extra.method% %extra.url% from %extra.ips% %context%\n", /* * Log message datetime format. * For for details see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format * and https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php */ 'date_format' => null, // "Y-m-d\TH:i:sP" /* * Log current memory usage * @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryUsageProcessor.php */ 'memory_usage' => true, /* * Log peak memory usage * @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryPeakUsageProcessor.php */ 'memory_peak_usage' => true, /* * Log current git branch and commit * @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/GitProcessor.php */ 'git' => true, /* * false - don't log body fields * ['only'] - log fields only * ['except'] - don't log fields * * If ['only'] is set, ['except'] parametr will be omitted */ // 'data' => false, 'data' => [ 'only' => [], 'except' => [], ], /* * false - don't log uploaded files * ['only'] - log files only * ['except'] - don't log files * * If ['only'] is set, ['except'] parametr will be omitted */ // 'files' => false, 'files' => [ 'only' => [], 'except' => [], ], /* * false - don't log headers * ['only'] - log headers only * ['except'] - don't log headers * * If ['only'] is set, ['except'] parametr will be omitted */ // 'headers' => false, 'headers' => [ 'only' => ['user-agent'], 'except' => [], ], /* * false - don't log session * ['only'] - log session only * ['except'] - don't log session * * If ['only'] is set, ['except'] parametr will be omitted */ 'session' => false, // 'session' => [ // 'only' => [], // 'except' => [], // ], ];
Usage
This packages provides a middleware which can be added as a global middleware or as a single route.
// in `app/Http/Kernel.php` protected $middleware = [ // ... \Chelout\HttpLogger\Middlewares\HttpLogger::class ];
// in a routes file Route::post('/submit-form', function () { // })->middleware(\Chelout\HttpLogger\Middlewares\HttpLogger::class);
In order to log http requests you should add log custom log channel:
// in config/logging.php return [ // ... 'channels' => [ // ... 'http-logger' => [ 'driver' => 'custom', 'via' => \Chelout\HttpLogger\Loggers\HttpLogger::class, ], ], ];
You can also enhance existing log channel by customizing Monolog configuration:
// in config/logging.php return [ // ... 'channels' => [ // ... 'single' => [ 'driver' => 'single', 'tap' => [Chelout\HttpLogger\Loggers\MonologCustomizer::class], 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', ], ], ];
Todo
- tests
- log git data?
- log memory usage?
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Inspiration
This package was inspired by Log HTTP requests and Laravel Log Enhancer and Laravel 5.6 logging servises.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.