|
| 1 | +# Loggers |
| 2 | + |
| 3 | +## Configuring a logger |
| 4 | + |
| 5 | +### Globally |
| 6 | + |
| 7 | +If you want to replace the default logger you will have to bind an instance in the service container. |
| 8 | + |
| 9 | +```php |
| 10 | +<?php |
| 11 | + |
| 12 | +namespace App\Providers; |
| 13 | + |
| 14 | +use Illuminate\Support\ServiceProvider; |
| 15 | +use HappyDemon\SaloonUtils\Logger\Contracts\Logger; |
| 16 | +use HappyDemon\SaloonUtils\Logger\Stores\DatabaseLogger; |
| 17 | + |
| 18 | +class AppServiceProvider extends ServiceProvider |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Register any application services. |
| 22 | + */ |
| 23 | + public function register(): void |
| 24 | + { |
| 25 | + $this->app->bind( |
| 26 | + Logger::class, |
| 27 | + fn (Application $application) => new DatabaseLogger |
| 28 | + ); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +``` |
| 33 | + |
| 34 | +### Locally |
| 35 | + |
| 36 | +If you have a special case for a specific connector, you could define which logger to use on the connector itself: |
| 37 | + |
| 38 | +```php |
| 39 | +<?php |
| 40 | + |
| 41 | +use HappyDemon\SaloonUtils\Logger\Contracts\Logger; |
| 42 | +use HappyDemon\SaloonUtils\Logger\Contracts\ProvidesLogger; |
| 43 | +use HappyDemon\SaloonUtils\Logger\LoggerPlugin; |
| 44 | +use HappyDemon\SaloonUtils\Logger\Stores\MemoryLogger; |
| 45 | +use Saloon\Http\Connector; |
| 46 | + |
| 47 | +class ForgeConnector extends Connector implements ProvidesLogger |
| 48 | +{ |
| 49 | + use LoggerPlugin; |
| 50 | + |
| 51 | + public function resolveBaseUrl(): string |
| 52 | + { |
| 53 | + return 'https://forge.laravel.com/api/v1'; |
| 54 | + } |
| 55 | + |
| 56 | + public static function setUpRequestLogger(): Logger |
| 57 | + { |
| 58 | + return new MemoryLogger |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Built in loggers |
| 64 | + |
| 65 | +### Database logger |
| 66 | + |
| 67 | +``` |
| 68 | +HappyDemon\SaloonUtils\Logger\Stores\DatabaseLogger |
| 69 | +``` |
| 70 | + |
| 71 | +When using the **default** built-in database logger, you'll have to publish & run migrations; |
| 72 | + |
| 73 | +```bash |
| 74 | +php artisan vendor:publish --tag saloon-utils.migrations |
| 75 | +php artisan migrate |
| 76 | +``` |
| 77 | + |
| 78 | +This logger will store each request in the `saloon_requests` table. |
| 79 | + |
| 80 | +{% hint style="info" %} |
| 81 | +Be sure to schedule model pruning daily with a cronjob |
| 82 | +{% endhint %} |
| 83 | + |
| 84 | +```php |
| 85 | +use HappyDemon\SaloonUtils\Logger\SaloonRequest; |
| 86 | +Schedule::command('model:prune', ['--model' => config('saloon-utils.logs.database_model')])->daily(); |
| 87 | +``` |
| 88 | + |
| 89 | +You are able to overwrite the model class altogether by defining your own model in the `saloon-utils.logs.database_model` config. |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +This is the default-bundled model: |
| 94 | + |
| 95 | +{% @github-files/github-code-block url="https://github.com/happyDemon/saloon-utils/blob/main/src/Logger/SaloonRequest.php" %} |
| 96 | + |
| 97 | +### Memory logger |
| 98 | + |
| 99 | +``` |
| 100 | +HappyDemon\SaloonUtils\Logger\Stores\MemoryLogger |
| 101 | +``` |
| 102 | + |
| 103 | +This logger can be helpful when debugging or running tests.\ |
| 104 | +It will setup a cache store under `saloon-utils` with the array driver. |
| 105 | + |
| 106 | +You can retrieve the requests that were sent on the logger itself: |
| 107 | + |
| 108 | +```php |
| 109 | +app(MemoryLogger::class)->logs(); |
| 110 | +(new MemoryLogger)->logs(); |
| 111 | +``` |
| 112 | + |
| 113 | +### Build your own |
| 114 | + |
| 115 | +You can easily build your own logger and set it as the default. |
| 116 | + |
| 117 | +Ensure your custom logger implements the `Logger` interface. |
| 118 | + |
| 119 | +{% hint style="info" %} |
| 120 | +Be sure to make use of the `HappyDemon\SaloonUtils\Logger\Stores\ParsesRequestData` trait when implementing your own logger. It provides helper methods for data conversion and redaction. |
| 121 | +{% endhint %} |
| 122 | + |
| 123 | +{% @github-files/github-code-block url="https://github.com/happyDemon/saloon-utils/blob/main/src/Logger/Contracts/Logger.php" visible="true" %} |
0 commit comments