From b22a4a916ecf4207d8d9120f0f3c39c659118f97 Mon Sep 17 00:00:00 2001 From: Pawel Trauth Date: Mon, 24 Aug 2020 10:25:12 +0100 Subject: [PATCH] fix: add stderr logging (#29) --- src/Console/Commands/EventStoreWorker.php | 11 +++++++++++ src/EventStore.php | 24 +++++++++++++++++++++-- src/ServiceProvider.php | 24 +++++++++++++++++++---- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/Console/Commands/EventStoreWorker.php b/src/Console/Commands/EventStoreWorker.php index 2dcacea..121e9df 100644 --- a/src/Console/Commands/EventStoreWorker.php +++ b/src/Console/Commands/EventStoreWorker.php @@ -74,6 +74,17 @@ public function handle(): void } } } + + $errorOutput = $entry['process']->getIncrementalErrorOutput(); + if (!empty($errorOutput)) { + foreach (explode(PHP_EOL, $errorOutput) as $line) { + $line = trim($line); + + if (!empty($line)) { + (LaravelEventStore::$workerErrorLogger)($line); + } + } + } } sleep(1); diff --git a/src/EventStore.php b/src/EventStore.php index ea2eaad..6e70c86 100644 --- a/src/EventStore.php +++ b/src/EventStore.php @@ -21,6 +21,13 @@ class EventStore */ public static $workerLogger; + /** + * Variable for logger. + * + * @var callable + */ + public static $workerErrorLogger; + /** * Variable for logger. * @@ -51,11 +58,24 @@ public static function eventToClass(?callable $callback = null) */ public static function workerLogger(?callable $logger = null) { - static::$workerLogger = $logger ?: function($message, $context = []){ + static::$workerLogger = $logger ?: function ($message, $context = []) { Log::info($message, $context); }; } + /** + * Set the logger environment. + * + * @param callable $callback + * @return void + */ + public static function workerErrorLogger(?callable $logger = null) + { + static::$workerErrorLogger = $logger ?: function ($message, $context = []) { + Log::error($message, $context); + }; + } + /** * Set the logger environment. * @@ -64,7 +84,7 @@ public static function workerLogger(?callable $logger = null) */ public static function threadLogger(?callable $logger = null) { - static::$threadLogger = $logger ?: function($message, $context = []){ + static::$threadLogger = $logger ?: function ($message, $context = []) { Log::channel('stdout')->info($message, $context); }; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index dabe31a..54c59ef 100755 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -33,6 +33,7 @@ public function boot() $this->eventClasses(); $this->threadLogger(); $this->workerLogger(); + $this->workerErrorLogger(); Event::listen(ShouldBeStored::class, SendToEventStoreListener::class); } @@ -44,8 +45,9 @@ public function boot() */ public function eventClasses() { - if (empty(EventStore::$eventToClass)) + if (empty(EventStore::$eventToClass)) { EventStore::eventToClass(); + } } /** @@ -55,19 +57,33 @@ public function eventClasses() */ public function threadLogger() { - if (empty(EventStore::$threadLogger)) + if (empty(EventStore::$threadLogger)) { EventStore::threadLogger(); + } } /** - * Handle logging when event is triggered. + * Handle passing of std::out output from thread * * @return void */ public function workerLogger() { - if (empty(EventStore::$workerLogger)) + if (empty(EventStore::$workerLogger)) { EventStore::workerLogger(); + } + } + + /** + * Handle passing of std:err output from thread + * + * @return void + */ + public function workerErrorLogger() + { + if (empty(EventStore::$workerErrorLogger)) { + EventStore::workerErrorLogger(); + } } /**