From 2a4a34122d913eb792a49ecb999c60b89fddbeb1 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Fri, 29 Mar 2019 12:07:24 -0500 Subject: [PATCH 1/7] lazy exception formatting --- src/Process/Job.php | 2 +- src/Process/Pool/Logger.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Process/Job.php b/src/Process/Job.php index c85734cb..aa77c3f6 100644 --- a/src/Process/Job.php +++ b/src/Process/Job.php @@ -28,7 +28,7 @@ protected function _run(): Forked $this->_getMaintainer()->deleteCompletedJobs(); $this->_getForeman()->workWorker(); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); $this->_setOrReplaceExitCode(255); } diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index 7d691773..fa9a6041 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -16,6 +16,7 @@ class Logger extends Log\AbstractLogger implements LoggerInterface use Defensive\AwareTrait; public const PROP_IS_ENABLED = 'is_enabled'; protected const LOG_DATE_TIME_FORMAT = 'D, d M y H:i:s.u T'; + const CONTEXT_KEY_EXCEPTION = 'exception'; protected $log_formatter; protected $level_filter_mask; @@ -49,6 +50,15 @@ public function log($level, $message, array $context = []) $logMessage->setProcessId($processId); $logMessage->setProcessPath($this->_getProcess()->getPath()); $logMessage->setMessage($message); + + if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] + instanceof \Throwable){ + $exceptionObject = $context[self::CONTEXT_KEY_EXCEPTION]; + unset($context[self::CONTEXT_KEY_EXCEPTION]); + $formattedException = ['exception_string'=> (string)$exceptionObject]; + $context['exception'] = $formattedException; + } + if (json_encode($context) === false) { $logMessage->setContext([]); } else { From 92af593e2c5e539b036f89b25eaf90c787d23505 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Fri, 29 Mar 2019 12:11:52 -0500 Subject: [PATCH 2/7] feature/KOJO-99 | monolog --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cb33768b..c64fea9e 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "symfony/finder": "^4.0", "zendframework/zend-db": "^2.8", "dragonmantank/cron-expression": "^2.0", - "doctrine/dbal": "^2.7" + "doctrine/dbal": "^2.7", + "monolog/monolog": "^1.24.0" }, "require-dev": { "phpunit/phpunit": "^7.0", From 2eed479d415e82ea74c87e7c0e9858a0af0af628 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Fri, 29 Mar 2019 16:54:29 -0500 Subject: [PATCH 3/7] use monolog normalizer --- src/Process/Pool/Logger.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index fa9a6041..b5960eb4 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -3,6 +3,7 @@ namespace Neighborhoods\Kojo\Process\Pool; +use Monolog\Formatter\NormalizerFormatter; use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface; use Neighborhoods\Kojo\ProcessInterface; use Neighborhoods\Pylon\Data\Property\Defensive; @@ -54,9 +55,10 @@ public function log($level, $message, array $context = []) if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] instanceof \Throwable){ $exceptionObject = $context[self::CONTEXT_KEY_EXCEPTION]; + $normalizedException = (new NormalizerFormatter())->format([$context[self::CONTEXT_KEY_EXCEPTION]]); unset($context[self::CONTEXT_KEY_EXCEPTION]); $formattedException = ['exception_string'=> (string)$exceptionObject]; - $context['exception'] = $formattedException; + $context['exception'] = $normalizedException; } if (json_encode($context) === false) { From f2b598955ddc31f4ecc0b230b7bf32e5f6c52d7e Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Sun, 31 Mar 2019 20:59:00 -0500 Subject: [PATCH 4/7] normalize the exception to an object not nested in an array --- src/Process/Pool/Logger.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index b5960eb4..e3b51248 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -54,11 +54,9 @@ public function log($level, $message, array $context = []) if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] instanceof \Throwable){ - $exceptionObject = $context[self::CONTEXT_KEY_EXCEPTION]; $normalizedException = (new NormalizerFormatter())->format([$context[self::CONTEXT_KEY_EXCEPTION]]); unset($context[self::CONTEXT_KEY_EXCEPTION]); - $formattedException = ['exception_string'=> (string)$exceptionObject]; - $context['exception'] = $normalizedException; + $context['exception'] = $normalizedException[0]; } if (json_encode($context) === false) { From 3c47384a0b9374f459732993a04b2c0b479c4ec7 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Sun, 31 Mar 2019 21:03:43 -0500 Subject: [PATCH 5/7] pass all the other exceptions as an object --- src/Message/Broker/Redis.php | 12 ++++++------ src/Process/Listener/Command.php | 2 +- src/Process/Pool/Strategy.php | 12 ++++++------ src/Process/Pool/Strategy/Server.php | 6 +++--- src/Process/Pool/Strategy/Worker.php | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Message/Broker/Redis.php b/src/Message/Broker/Redis.php index d255362f..c4e5681b 100644 --- a/src/Message/Broker/Redis.php +++ b/src/Message/Broker/Redis.php @@ -21,7 +21,7 @@ public function waitForNewMessage(): BrokerInterface 0 ); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -43,7 +43,7 @@ public function hasMessage(): bool $publishChannelLength = $this->getPublishChannelLength(); $subscriptionChannelLength = $this->getSubscriptionChannelLength(); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -58,7 +58,7 @@ public function getNextMessage(): string $message = $this->_getRedisClient()->rPop($this->_getPublishChannelName()); } } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -70,7 +70,7 @@ public function getPublishChannelLength(): int try { $publishChannelLength = $this->_getRedisClient()->lLen($this->_getPublishChannelName()); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -82,7 +82,7 @@ public function getSubscriptionChannelLength(): int try { $subscriptionChannelLength = $this->_getRedisClient()->lLen($this->_getSubscriptionChannelName()); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } @@ -94,7 +94,7 @@ public function publishMessage($message): BrokerInterface try { $this->_getRedisClient()->lPush($this->_getPublishChannelName(), $message); } catch (\Throwable $throwable) { - $this->_getLogger()->critical($throwable->getMessage(), [(string)$throwable]); + $this->_getLogger()->critical($throwable->getMessage(), ['exception' => $throwable]); throw $throwable; } diff --git a/src/Process/Listener/Command.php b/src/Process/Listener/Command.php index c1cd9d99..078f13c5 100644 --- a/src/Process/Listener/Command.php +++ b/src/Process/Listener/Command.php @@ -41,7 +41,7 @@ public function addProcess(string $processTypeCode): Command try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } return $this; diff --git a/src/Process/Pool/Strategy.php b/src/Process/Pool/Strategy.php index abd24e58..0b1d88cc 100644 --- a/src/Process/Pool/Strategy.php +++ b/src/Process/Pool/Strategy.php @@ -50,7 +50,7 @@ protected function _listenerProcessExited(ListenerInterface $listenerProcess): S $this->_getProcessPool()->addChildProcess($replacementListenerProcess); } catch (Exception $forkedException) { $this->_pauseListenerProcess($listenerProcess); - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } @@ -85,7 +85,7 @@ protected function _jobProcessExited(JobInterface $jobProcess): Strategy try { $this->_getProcessPool()->addChildProcess($replacementProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } if (!$this->_getProcessPool()->hasAlarm()) { $this->_getProcessPool()->setAlarm($this->getMaxAlarmTime()); @@ -106,7 +106,7 @@ public function receivedAlarm(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($alarmProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } @@ -125,7 +125,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); if ($process instanceof CommandInterface) { $this->_getProcessPool()->getProcess()->exit(); } @@ -138,7 +138,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($fillProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } @@ -179,7 +179,7 @@ protected function _unPauseListenerProcesses(): Strategy $this->_getProcessPool()->addChildProcess($newListenerProcess); unset($this->_pausedListenerProcesses[$processId]); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } } else { diff --git a/src/Process/Pool/Strategy/Server.php b/src/Process/Pool/Strategy/Server.php index 9e998dae..40d4b7a9 100644 --- a/src/Process/Pool/Strategy/Server.php +++ b/src/Process/Pool/Strategy/Server.php @@ -21,7 +21,7 @@ public function childProcessExited(ProcessInterface $process): StrategyInterface try { $this->_getProcessPool()->addChildProcess($rootProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); } } else { $className = get_class($process); @@ -48,7 +48,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } } @@ -59,7 +59,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($fillProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } } diff --git a/src/Process/Pool/Strategy/Worker.php b/src/Process/Pool/Strategy/Worker.php index c6076a53..25e597b2 100644 --- a/src/Process/Pool/Strategy/Worker.php +++ b/src/Process/Pool/Strategy/Worker.php @@ -52,7 +52,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($process); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } } @@ -63,7 +63,7 @@ public function initializePool(): StrategyInterface try { $this->_getProcessPool()->addChildProcess($fillProcess); } catch (Exception $forkedException) { - $this->_getLogger()->critical($forkedException->getMessage(), [(string)$forkedException]); + $this->_getLogger()->critical($forkedException->getMessage(), ['exception' => $forkedException]); $this->_getProcessPool()->getProcess()->exit(); } } From dde0167a88f0c2288cb46ba2a28de183f4b76b66 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Sun, 31 Mar 2019 21:08:50 -0500 Subject: [PATCH 6/7] throw on an exception_string that everyone knows and loves --- src/Process/Pool/Logger.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index e3b51248..2ead011c 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -54,6 +54,7 @@ public function log($level, $message, array $context = []) if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] instanceof \Throwable){ + $context['exception_sting'] = (string)$context[self::CONTEXT_KEY_EXCEPTION]; $normalizedException = (new NormalizerFormatter())->format([$context[self::CONTEXT_KEY_EXCEPTION]]); unset($context[self::CONTEXT_KEY_EXCEPTION]); $context['exception'] = $normalizedException[0]; From 38b19bb01442bd932d0a499f70d98a429bf44d05 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Mon, 1 Apr 2019 10:02:27 -0500 Subject: [PATCH 7/7] take the sting out of using strings --- src/Process/Pool/Logger.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index 2ead011c..b2c5f63b 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -15,9 +15,12 @@ class Logger extends Log\AbstractLogger implements LoggerInterface use Time\AwareTrait; use Logger\Message\Factory\AwareTrait; use Defensive\AwareTrait; + public const PROP_IS_ENABLED = 'is_enabled'; + public const CONTEXT_KEY_EXCEPTION = 'exception'; + public const CONTEXT_KEY_EXCEPTION_STRING = 'exception_string'; + protected const LOG_DATE_TIME_FORMAT = 'D, d M y H:i:s.u T'; - const CONTEXT_KEY_EXCEPTION = 'exception'; protected $log_formatter; protected $level_filter_mask; @@ -54,10 +57,10 @@ public function log($level, $message, array $context = []) if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] instanceof \Throwable){ - $context['exception_sting'] = (string)$context[self::CONTEXT_KEY_EXCEPTION]; + $context[self::CONTEXT_KEY_EXCEPTION_STRING] = (string)$context[self::CONTEXT_KEY_EXCEPTION]; $normalizedException = (new NormalizerFormatter())->format([$context[self::CONTEXT_KEY_EXCEPTION]]); unset($context[self::CONTEXT_KEY_EXCEPTION]); - $context['exception'] = $normalizedException[0]; + $context[self::CONTEXT_KEY_EXCEPTION] = $normalizedException[0]; } if (json_encode($context) === false) {