From a92dd65e0e635bca74e656018b33d71d503596b7 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Thu, 26 Jun 2014 14:00:30 +0400 Subject: [PATCH 1/2] Fixes #3101: Improved handling of log target failures. It will now skip target and log reason instead of going into infinite cycle --- framework/CHANGELOG.md | 1 + framework/log/Target.php | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 16382b6..be0ecaa 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -73,6 +73,7 @@ Yii Framework 2 Change Log - Enh #2942: Added truncate and truncateWord methods (Alex-Code, samdark) - Enh #3008: Added `Html::errorSummary()` (qiangxue) - Enh #3088: The debug and gii modules will manage their own URL rules now (hiltonjanfield, qiangxue) +- Enh #3101: Improved handling of log target failures. It will now skip target and log reason instead of going into infinite cycle (samdark) - Enh #3103: debugger panel is now not displayed when printing a page (githubjeka) - Enh #3108: Added `yii\debug\Module::enableDebugLogs` to disable logging debug logs by default (qiangxue) - Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue) diff --git a/framework/log/Target.php b/framework/log/Target.php index c07a501..7f1e6dc 100644 --- a/framework/log/Target.php +++ b/framework/log/Target.php @@ -105,7 +105,13 @@ abstract class Target extends Component if (($context = $this->getContextMessage()) !== '') { $this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME]; } - $this->export(); + try { + $this->export(); + } catch (\Exception $e) { + $this->enabled = false; + \Yii::warning('Unable to send log via '. get_class($this) .': ' . $e->getMessage(), __METHOD__); + \Yii::getLogger()->flush(true); + } $this->messages = []; } } From 38c056c736c124b8df10ba4536ac912429c5a0bd Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Thu, 26 Jun 2014 15:01:02 +0400 Subject: [PATCH 2/2] Moved handling from Target to Dispatcher, fixed edge cases --- framework/log/Dispatcher.php | 18 +++++++++++++++++- framework/log/Target.php | 8 +------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/framework/log/Dispatcher.php b/framework/log/Dispatcher.php index eda796d..00a260b 100644 --- a/framework/log/Dispatcher.php +++ b/framework/log/Dispatcher.php @@ -175,10 +175,26 @@ class Dispatcher extends Component */ public function dispatch($messages, $final) { + $targetErrors = []; foreach ($this->targets as $target) { if ($target->enabled) { - $target->collect($messages, $final); + try { + $target->collect($messages, $final); + } catch (\Exception $e) { + $target->enabled = false; + $targetErrors[] = [ + 'Unable to send log via '. get_class($target) .': ' . $e->getMessage(), + Logger::LEVEL_WARNING, + __METHOD__, + microtime(true), + [], + ]; + } } } + + if (!empty($targetErrors)) { + $this->dispatch($targetErrors, true); + } } } diff --git a/framework/log/Target.php b/framework/log/Target.php index 7f1e6dc..c07a501 100644 --- a/framework/log/Target.php +++ b/framework/log/Target.php @@ -105,13 +105,7 @@ abstract class Target extends Component if (($context = $this->getContextMessage()) !== '') { $this->messages[] = [$context, Logger::LEVEL_INFO, 'application', YII_BEGIN_TIME]; } - try { - $this->export(); - } catch (\Exception $e) { - $this->enabled = false; - \Yii::warning('Unable to send log via '. get_class($this) .': ' . $e->getMessage(), __METHOD__); - \Yii::getLogger()->flush(true); - } + $this->export(); $this->messages = []; } }