From 2dd16266c1c3168068f088154052434dc3200fec Mon Sep 17 00:00:00 2001 From: Sergei Baikin Date: Thu, 14 Jul 2022 08:49:05 +0200 Subject: [PATCH] Laravel add processors (#1) * Add custom processors * Update documentation * Up composer version Co-authored-by: sergei.baikin --- README.md | 6 ++++-- composer.json | 2 +- src/Laravel/LaravelLoggerCreating.php | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3b07390..b174a64 100644 --- a/README.md +++ b/README.md @@ -61,13 +61,15 @@ add in `config/logging.php` in `channels` section: 'driver' => 'custom', 'via' => new Gotphoto\Logging\Laravel\LaravelLoggerCreating, 'app_name' => 'ServiceName', - 'channel' => 'app'(security/reauest/order) + 'channel' => 'app'(security/reauest/order), + 'processors' => [new Monolog\Processor\ProcessorInterface()], //OPTIONAL ] 'security' => [ 'driver' => 'custom', 'via' => new Gotphoto\Logging\Laravel\LaravelLoggerCreating, 'app_name' => 'ServiceName', - 'channel' => 'security' + 'channel' => 'security', + 'processors' => [new Monolog\Processor\ProcessorInterface()], //OPTIONAL ], ``` diff --git a/composer.json b/composer.json index 8d7606e..5ba2a6c 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "fotografde/logging", "description": "Integrate a common way for log handling.", "type": "library", - "version": "1.0.3", + "version": "1.1.0", "require": { "php": "^7|^8", "monolog/monolog": "^2.7|^3.1", diff --git a/src/Laravel/LaravelLoggerCreating.php b/src/Laravel/LaravelLoggerCreating.php index cde1925..676f782 100644 --- a/src/Laravel/LaravelLoggerCreating.php +++ b/src/Laravel/LaravelLoggerCreating.php @@ -4,7 +4,11 @@ namespace Gotphoto\Logging\Laravel; +use Aws\Exception\AwsException; +use Gotphoto\Logging\ExceptionContext\AwsExceptionContext; +use Gotphoto\Logging\ExceptionContext\GuzzleRequestExceptionContext; use Gotphoto\Logging\Formatter; +use GuzzleHttp\Exception\RequestException; use Illuminate\Support\Facades\App; use Monolog\Handler\StreamHandler; use Monolog\Logger; @@ -16,18 +20,28 @@ public function __invoke(array $config) { assert(!empty($config['app_name']) && is_string($config['app_name'])); assert(!empty($config['channel']) && is_string($config['channel'])); + assert(!isset($config['processors']) || is_array($config['processors'])); $appName = $config['app_name']; $channel = $config['channel']; + /** @var \Monolog\Processor\ProcessorInterface[] $processors */ + $processors = $config['processors'] ?? []; $log = new Logger($channel); $log->pushProcessor(new Processor()); + foreach ($processors as $processor) { + $log->pushProcessor($processor); + } + $streamHandler = new StreamHandler('php://stderr'); $handler = $streamHandler; $env = App::environment(); $handler->setFormatter( - new Formatter($appName, (is_string($env) ? $env : "undefined"), []) + new Formatter($appName, (is_string($env) ? $env : "undefined"), [ + RequestException::class => [new GuzzleRequestExceptionContext()], + AwsException::class => [new AwsExceptionContext()], + ]) ); $log->pushHandler($handler);