Added config of processors for logger. (#1361)

* Added processors for logger.

* Added config of default processor for logger.
This commit is contained in:
李铭昕 2020-02-24 16:30:22 +08:00 committed by GitHub
parent 756572f7c7
commit 996e3f1242
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 208 additions and 1 deletions

View File

@ -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

View File

@ -25,5 +25,6 @@ return [
],
],
],
'processors' => [],
],
];

View File

@ -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'] ?? [[]];

View File

@ -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,
],
],
],
],
]));

View File

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Logger\Stub;
class BarProcessor
{
public function __invoke(array $records)
{
$records['bar'] = true;
return $records;
}
}

View File

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Logger\Stub;
use Hyperf\Utils\Context;
use Monolog\Handler\StreamHandler;
class FooHandler extends StreamHandler
{
public function write(array $record)
{
Context::set('test.logger.foo_handler.record', $record);
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Logger\Stub;
use Monolog\Processor\ProcessorInterface;
class FooProcessor implements ProcessorInterface
{
protected $repeat;
public function __construct(int $repeat)
{
$this->repeat = 2;
}
public function __invoke(array $records)
{
$records['message'] = str_repeat($records['message'], $this->repeat);
return $records;
}
}