diff options
author | Morris Jobke <hey@morrisjobke.de> | 2019-05-09 15:27:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-09 15:27:05 +0200 |
commit | 55c3c91963f16443d5978f8365bd9589acce373c (patch) | |
tree | a53cbd7d586b4ffbab7d290b29b454127da13628 | |
parent | 38efad788194f3a700ac7a6d86708e0968d86e1b (diff) | |
parent | d49ee41fe316321c03d3bc9bc197647fd7ddcee2 (diff) | |
download | nextcloud-server-55c3c91963f16443d5978f8365bd9589acce373c.tar.gz nextcloud-server-55c3c91963f16443d5978f8365bd9589acce373c.zip |
Merge pull request #15475 from nextcloud/enhancement/crash-report-message
Allow crash reporters to catpture any message
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/private/Log.php | 19 | ||||
-rw-r--r-- | lib/private/Support/CrashReport/Registry.php | 23 | ||||
-rw-r--r-- | lib/public/Support/CrashReport/IMessageReporter.php | 43 | ||||
-rw-r--r-- | lib/public/Support/CrashReport/IRegistry.php | 24 | ||||
-rw-r--r-- | tests/lib/Support/CrashReport/RegistryTest.php | 14 |
7 files changed, 114 insertions, 11 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index cfb8552d39a..2ff878cd4dc 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -389,6 +389,7 @@ return array( 'OCP\\Share_Backend_Collection' => $baseDir . '/lib/public/Share_Backend_Collection.php', 'OCP\\Share_Backend_File_Dependent' => $baseDir . '/lib/public/Share_Backend_File_Dependent.php', 'OCP\\Support\\CrashReport\\ICollectBreadcrumbs' => $baseDir . '/lib/public/Support/CrashReport/ICollectBreadcrumbs.php', + 'OCP\\Support\\CrashReport\\IMessageReporter' => $baseDir . '/lib/public/Support/CrashReport/IMessageReporter.php', 'OCP\\Support\\CrashReport\\IRegistry' => $baseDir . '/lib/public/Support/CrashReport/IRegistry.php', 'OCP\\Support\\CrashReport\\IReporter' => $baseDir . '/lib/public/Support/CrashReport/IReporter.php', 'OCP\\SystemTag\\ISystemTag' => $baseDir . '/lib/public/SystemTag/ISystemTag.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index bff8aa1af6b..365a073bcac 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -419,6 +419,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Share_Backend_Collection' => __DIR__ . '/../../..' . '/lib/public/Share_Backend_Collection.php', 'OCP\\Share_Backend_File_Dependent' => __DIR__ . '/../../..' . '/lib/public/Share_Backend_File_Dependent.php', 'OCP\\Support\\CrashReport\\ICollectBreadcrumbs' => __DIR__ . '/../../..' . '/lib/public/Support/CrashReport/ICollectBreadcrumbs.php', + 'OCP\\Support\\CrashReport\\IMessageReporter' => __DIR__ . '/../../..' . '/lib/public/Support/CrashReport/IMessageReporter.php', 'OCP\\Support\\CrashReport\\IRegistry' => __DIR__ . '/../../..' . '/lib/public/Support/CrashReport/IRegistry.php', 'OCP\\Support\\CrashReport\\IReporter' => __DIR__ . '/../../..' . '/lib/public/Support/CrashReport/IReporter.php', 'OCP\\SystemTag\\ISystemTag' => __DIR__ . '/../../..' . '/lib/public/SystemTag/ISystemTag.php', diff --git a/lib/private/Log.php b/lib/private/Log.php index 43ff4cc9dee..4af833d778f 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -35,6 +35,7 @@ declare(strict_types=1); namespace OC; +use function array_merge; use InterfaSys\LogNormalizer\Normalizer; use OC\Log\ExceptionSerializer; @@ -42,7 +43,6 @@ use OCP\Log\IFileBased; use OCP\Log\IWriter; use OCP\ILogger; use OCP\Support\CrashReport\IRegistry; -use OCP\Util; /** * logging utilities @@ -216,11 +216,22 @@ class Log implements ILogger { if ($level >= $minLevel) { $this->writeLog($app, $message, $level); - } - if (!is_null($this->crashReporters)) { - $this->crashReporters->delegateBreadcrumb($message, 'log', $context); + if ($this->crashReporters !== null) { + $messageContext = array_merge( + $context, + [ + 'level' => $level + ] + ); + $this->crashReporters->delegateMessage($message, $messageContext); + } + } else { + if ($this->crashReporters !== null) { + $this->crashReporters->delegateBreadcrumb($message, 'log', $context); + } } + } private function getLogLevel($context) { diff --git a/lib/private/Support/CrashReport/Registry.php b/lib/private/Support/CrashReport/Registry.php index 72c43fe37e2..be022efb385 100644 --- a/lib/private/Support/CrashReport/Registry.php +++ b/lib/private/Support/CrashReport/Registry.php @@ -24,6 +24,7 @@ namespace OC\Support\CrashReport; use Exception; use OCP\Support\CrashReport\ICollectBreadcrumbs; +use OCP\Support\CrashReport\IMessageReporter; use OCP\Support\CrashReport\IRegistry; use OCP\Support\CrashReport\IReporter; use Throwable; @@ -38,7 +39,7 @@ class Registry implements IRegistry { * * @param IReporter $reporter */ - public function register(IReporter $reporter) { + public function register(IReporter $reporter): void { $this->reporters[] = $reporter; } @@ -51,7 +52,7 @@ class Registry implements IRegistry { * * @since 15.0.0 */ - public function delegateBreadcrumb(string $message, string $category, array $context = []) { + public function delegateBreadcrumb(string $message, string $category, array $context = []): void { foreach ($this->reporters as $reporter) { if ($reporter instanceof ICollectBreadcrumbs) { $reporter->collect($message, $category, $context); @@ -65,11 +66,27 @@ class Registry implements IRegistry { * @param Exception|Throwable $exception * @param array $context */ - public function delegateReport($exception, array $context = []) { + public function delegateReport($exception, array $context = []): void { /** @var IReporter $reporter */ foreach ($this->reporters as $reporter) { $reporter->report($exception, $context); } } + /** + * Delegate a message to all reporters that implement IMessageReporter + * + * @param string $message + * @param array $context + * + * @return void + */ + public function delegateMessage(string $message, array $context = []): void { + foreach ($this->reporters as $reporter) { + if ($reporter instanceof IMessageReporter) { + $reporter->reportMessage($message, $context); + } + } + } + } diff --git a/lib/public/Support/CrashReport/IMessageReporter.php b/lib/public/Support/CrashReport/IMessageReporter.php new file mode 100644 index 00000000000..1085aa13df3 --- /dev/null +++ b/lib/public/Support/CrashReport/IMessageReporter.php @@ -0,0 +1,43 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCP\Support\CrashReport; + +/** + * @since 17.0.0 + */ +interface IMessageReporter extends IReporter { + + /** + * Report a (error) message + * + * @param string $message + * @param array $context + * + * @since 17.0.0 + */ + public function reportMessage(string $message, array $context = []): void; + +} diff --git a/lib/public/Support/CrashReport/IRegistry.php b/lib/public/Support/CrashReport/IRegistry.php index 2833e66a9b7..814b9c8e3b9 100644 --- a/lib/public/Support/CrashReport/IRegistry.php +++ b/lib/public/Support/CrashReport/IRegistry.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + /** * @author Christoph Wurst <christoph@winzerhof-wurst.at> * @@ -33,10 +35,11 @@ interface IRegistry { /** * Register a reporter instance * - * @since 13.0.0 * @param IReporter $reporter + * + * @since 13.0.0 */ - public function register(IReporter $reporter); + public function register(IReporter $reporter): void; /** * Delegate breadcrumb collection to all registered reporters @@ -47,14 +50,27 @@ interface IRegistry { * * @since 15.0.0 */ - public function delegateBreadcrumb(string $message, string $category, array $context = []); + public function delegateBreadcrumb(string $message, string $category, array $context = []): void; /** * Delegate crash reporting to all registered reporters * - * @since 13.0.0 * @param Exception|Throwable $exception * @param array $context + * + * @since 13.0.0 */ public function delegateReport($exception, array $context = []); + + /** + * Delegate a message to all reporters that implement IMessageReporter + * + * @param string $message + * @param array $context + * + * @return void + * + * @since 17.0.0 + */ + public function delegateMessage(string $message, array $context = []): void; } diff --git a/tests/lib/Support/CrashReport/RegistryTest.php b/tests/lib/Support/CrashReport/RegistryTest.php index 5cb15db7345..be769e257fc 100644 --- a/tests/lib/Support/CrashReport/RegistryTest.php +++ b/tests/lib/Support/CrashReport/RegistryTest.php @@ -27,6 +27,7 @@ namespace Test\Support\CrashReport; use Exception; use OC\Support\CrashReport\Registry; use OCP\Support\CrashReport\ICollectBreadcrumbs; +use OCP\Support\CrashReport\IMessageReporter; use OCP\Support\CrashReport\IReporter; use Test\TestCase; @@ -81,4 +82,17 @@ class RegistryTest extends TestCase { $this->registry->delegateReport($exception); } + public function testDelegateMessage() { + $reporter1 = $this->createMock(IReporter::class); + $reporter2 = $this->createMock(IMessageReporter::class); + $message = 'hello'; + $reporter2->expects($this->once()) + ->method('reportMessage') + ->with($message, []); + + $this->registry->register($reporter1); + $this->registry->register($reporter2); + $this->registry->delegateMessage($message); + } + } |