From 996e3f1242e9b13fae7b3c2fce76095f87646ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= Date: Mon, 24 Feb 2020 16:30:22 +0800 Subject: [PATCH] Added config of `processors` for logger. (#1361) * Added processors for logger. * Added config of default processor for logger. --- CHANGELOG.md | 3 +- src/logger/publish/logger.php | 1 + src/logger/src/LoggerFactory.php | 20 +++++ src/logger/tests/LoggerFactoryTest.php | 108 +++++++++++++++++++++++++ src/logger/tests/Stub/BarProcessor.php | 22 +++++ src/logger/tests/Stub/FooHandler.php | 24 ++++++ src/logger/tests/Stub/FooProcessor.php | 31 +++++++ 7 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 src/logger/tests/Stub/BarProcessor.php create mode 100644 src/logger/tests/Stub/FooHandler.php create mode 100644 src/logger/tests/Stub/FooProcessor.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c85cd2600..4cd54a8bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [#1328](https://github.com/hyperf/hyperf/pull/1328) Added `ModelRewriteInheritanceVisitor` to rewrite the model inheritance for command `gen:model`. - [#1331](https://github.com/hyperf/hyperf/pull/1331) Added `Hyperf\LoadBalancer\LoadBalancerInterface::getNodes()`. - [#1335](https://github.com/hyperf/hyperf/pull/1335) Added event `AfterExecute` for `command`. +- [#1361](https://github.com/hyperf/hyperf/pull/1361) Added config of `processors` for logger. ## Changed @@ -23,7 +24,7 @@ ## Fixed - [#1330](https://github.com/hyperf/hyperf/pull/1330) Fixed bug when using `(new Parallel())->add($callback, $key)` and the parameter `$key` is a not string index, the returned result will sort `$key` from 0. -- [#1388](https://github.com/hyperf/hyperf/pull/1338) Fixed bug that root settings do not works when the slave servers set their own settings. +- [#1338](https://github.com/hyperf/hyperf/pull/1338) Fixed bug that root settings do not works when the slave servers set their own settings. - [#1344](https://github.com/hyperf/hyperf/pull/1344) Fixed bug that queue length check every time when not set max messages. # v1.1.17 - 2020-01-24 diff --git a/src/logger/publish/logger.php b/src/logger/publish/logger.php index 0976980ad..62ba382bf 100644 --- a/src/logger/publish/logger.php +++ b/src/logger/publish/logger.php @@ -25,5 +25,6 @@ return [ ], ], ], + 'processors' => [], ], ]; diff --git a/src/logger/src/LoggerFactory.php b/src/logger/src/LoggerFactory.php index eb6978904..3106da363 100644 --- a/src/logger/src/LoggerFactory.php +++ b/src/logger/src/LoggerFactory.php @@ -54,10 +54,12 @@ class LoggerFactory $config = $config[$group]; $handlers = $this->handlers($config); + $processors = $this->processors($config); return make(Logger::class, [ 'name' => $name, 'handlers' => $handlers, + 'processors' => $processors, ]); } @@ -95,6 +97,24 @@ class LoggerFactory ]; } + protected function processors(array $config): array + { + $result = []; + if (! isset($config['processors']) && isset($config['processor'])) { + $config['processors'] = [$config['processor']]; + } + + foreach ($config['processors'] ?? [] as $value) { + if (is_array($value) && isset($value['class'])) { + $value = make($value['class'], $value['constructor'] ?? []); + } + + $result[] = $value; + } + + return $result; + } + protected function handlers(array $config): array { $handlerConfigs = $config['handlers'] ?? [[]]; diff --git a/src/logger/tests/LoggerFactoryTest.php b/src/logger/tests/LoggerFactoryTest.php index d77db09e8..d8a97ba9c 100644 --- a/src/logger/tests/LoggerFactoryTest.php +++ b/src/logger/tests/LoggerFactoryTest.php @@ -16,6 +16,10 @@ use Hyperf\Config\Config; use Hyperf\Contract\ConfigInterface; use Hyperf\Logger\LoggerFactory; use Hyperf\Utils\ApplicationContext; +use Hyperf\Utils\Context; +use HyperfTest\Logger\Stub\BarProcessor; +use HyperfTest\Logger\Stub\FooHandler; +use HyperfTest\Logger\Stub\FooProcessor; use Mockery; use Monolog\Handler\StreamHandler; use Monolog\Handler\TestHandler; @@ -32,6 +36,7 @@ class LoggerFactoryTest extends TestCase public function tearDown() { Mockery::close(); + Context::set('test.logger.foo_handler.record', null); } public function testInvokeLoggerFactory() @@ -103,6 +108,60 @@ class LoggerFactoryTest extends TestCase $this->assertInstanceOf(TestHandler::class, $handlers[1]); } + public function testNotSetProcessor() + { + $container = $this->mockContainer(); + $factory = $container->get(LoggerFactory::class); + $logger = $factory->get('hyperf'); + $reflectionClass = new ReflectionClass($logger); + $handlersProperty = $reflectionClass->getProperty('processors'); + $handlersProperty->setAccessible(true); + $processors = $handlersProperty->getValue($logger); + $this->assertSame([], $processors); + } + + public function testProcessor() + { + $container = $this->mockContainer(); + $factory = $container->get(LoggerFactory::class); + $logger = $factory->get('hyperf', 'processor-test'); + $reflectionClass = new ReflectionClass($logger); + $handlersProperty = $reflectionClass->getProperty('processors'); + $handlersProperty->setAccessible(true); + $processors = $handlersProperty->getValue($logger); + $this->assertSame(3, count($processors)); + $this->assertInstanceOf(FooProcessor::class, $processors[0]); + + $logger->info('Hello world.'); + + $this->assertSame( + 'Hello world.Hello world.', + Context::get('test.logger.foo_handler.record')['message'] + ); + $this->assertTrue(Context::get('test.logger.foo_handler.record')['bar']); + $this->assertTrue(Context::get('test.logger.foo_handler.record')['callback']); + } + + public function testDefaultProcessor() + { + $container = $this->mockContainer(); + $factory = $container->get(LoggerFactory::class); + $logger = $factory->get('hyperf', 'default-processor'); + $reflectionClass = new ReflectionClass($logger); + $handlersProperty = $reflectionClass->getProperty('processors'); + $handlersProperty->setAccessible(true); + $processors = $handlersProperty->getValue($logger); + $this->assertSame(1, count($processors)); + $this->assertInstanceOf(FooProcessor::class, $processors[0]); + + $logger->info('Hello world.'); + + $this->assertSame( + 'Hello world.Hello world.', + Context::get('test.logger.foo_handler.record')['message'] + ); + } + private function mockContainer(): ContainerInterface { $container = Mockery::mock(ContainerInterface::class); @@ -149,6 +208,55 @@ class LoggerFactoryTest extends TestCase 'constructor' => [], ], ], + 'processor-test' => [ + 'handlers' => [ + [ + 'class' => FooHandler::class, + 'constructor' => [ + 'stream' => BASE_PATH . '/runtime/logs/hyperf.log', + 'level' => \Monolog\Logger::DEBUG, + ], + 'formatter' => [ + 'class' => \Monolog\Formatter\LineFormatter::class, + ], + ], + ], + 'processors' => [ + [ + 'class' => FooProcessor::class, + 'constructor' => [ + 'repeat' => 2, + ], + ], + [ + 'class' => BarProcessor::class, + ], + function (array $records) { + $records['callback'] = true; + return $records; + }, + ], + ], + 'default-processor' => [ + 'handlers' => [ + [ + 'class' => FooHandler::class, + 'constructor' => [ + 'stream' => BASE_PATH . '/runtime/logs/hyperf.log', + 'level' => \Monolog\Logger::DEBUG, + ], + 'formatter' => [ + 'class' => \Monolog\Formatter\LineFormatter::class, + ], + ], + ], + 'processor' => [ + 'class' => FooProcessor::class, + 'constructor' => [ + 'repeat' => 2, + ], + ], + ], ], ])); diff --git a/src/logger/tests/Stub/BarProcessor.php b/src/logger/tests/Stub/BarProcessor.php new file mode 100644 index 000000000..bd3b6688e --- /dev/null +++ b/src/logger/tests/Stub/BarProcessor.php @@ -0,0 +1,22 @@ +repeat = 2; + } + + public function __invoke(array $records) + { + $records['message'] = str_repeat($records['message'], $this->repeat); + return $records; + } +}