mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Simplified the handlers definition of logger (#5973)
Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
parent
9a59c57f37
commit
dc331072b7
@ -49,6 +49,7 @@
|
||||
- [#5948](https://github.com/hyperf/hyperf/pull/5948) Optimized `Hyperf\Coroutine\Locker`.
|
||||
- [#5960](https://github.com/hyperf/hyperf/pull/5960) Allowed set poolName in Annotation.
|
||||
- [#5972](https://github.com/hyperf/hyperf/pull/5972) `Collection::except()` with null returns all.
|
||||
- [#5973](https://github.com/hyperf/hyperf/pull/5973) Simplified the handlers definition of logger.
|
||||
|
||||
## Removed
|
||||
|
||||
|
@ -4,7 +4,7 @@ The `hyperf/logger` component is implemented based on [psr/logger](https://githu
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
```shell
|
||||
composer require hyperf/logger
|
||||
```
|
||||
|
||||
@ -308,9 +308,62 @@ return [
|
||||
|
||||
```
|
||||
|
||||
The result is as follows
|
||||
Or
|
||||
|
||||
```php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Monolog\Handler;
|
||||
use Monolog\Formatter;
|
||||
use Monolog\Logger;
|
||||
|
||||
return [
|
||||
'default' => [
|
||||
'handlers' => ['single', 'daily'],
|
||||
],
|
||||
|
||||
'single' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Logger::INFO,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\LineFormatter::class,
|
||||
'constructor' => [
|
||||
'format' => null,
|
||||
'dateFormat' => null,
|
||||
'allowInlineLineBreaks' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
'daily' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf-debug.log',
|
||||
'level' => Logger::DEBUG,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\JsonFormatter::class,
|
||||
'constructor' => [
|
||||
'batchMode' => Formatter\JsonFormatter::BATCH_MODE_JSON,
|
||||
'appendNewline' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
The result is as follows
|
||||
|
||||
```shell
|
||||
==> runtime/logs/hyperf.log <==
|
||||
[2019-11-08 11:11:35] hyperf.INFO: 5dc4dce791690 [] []
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
## 安装
|
||||
|
||||
```
|
||||
```shell
|
||||
composer require hyperf/logger
|
||||
```
|
||||
|
||||
@ -305,9 +305,62 @@ return [
|
||||
];
|
||||
```
|
||||
|
||||
结果如下
|
||||
或
|
||||
|
||||
```php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Monolog\Handler;
|
||||
use Monolog\Formatter;
|
||||
use Monolog\Logger;
|
||||
|
||||
return [
|
||||
'default' => [
|
||||
'handlers' => ['single', 'daily'],
|
||||
],
|
||||
|
||||
'single' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Logger::INFO,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\LineFormatter::class,
|
||||
'constructor' => [
|
||||
'format' => null,
|
||||
'dateFormat' => null,
|
||||
'allowInlineLineBreaks' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
'daily' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf-debug.log',
|
||||
'level' => Logger::DEBUG,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\JsonFormatter::class,
|
||||
'constructor' => [
|
||||
'batchMode' => Formatter\JsonFormatter::BATCH_MODE_JSON,
|
||||
'appendNewline' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
结果如下
|
||||
|
||||
```shell
|
||||
==> runtime/logs/hyperf.log <==
|
||||
[2019-11-08 11:11:35] hyperf.INFO: 5dc4dce791690 [] []
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
## 安裝
|
||||
|
||||
```
|
||||
```shell
|
||||
composer require hyperf/logger
|
||||
```
|
||||
|
||||
@ -305,9 +305,62 @@ return [
|
||||
];
|
||||
```
|
||||
|
||||
結果如下
|
||||
或
|
||||
|
||||
```php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Monolog\Handler;
|
||||
use Monolog\Formatter;
|
||||
use Monolog\Logger;
|
||||
|
||||
return [
|
||||
'default' => [
|
||||
'handlers' => ['single', 'daily'],
|
||||
],
|
||||
|
||||
'single' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Logger::INFO,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\LineFormatter::class,
|
||||
'constructor' => [
|
||||
'format' => null,
|
||||
'dateFormat' => null,
|
||||
'allowInlineLineBreaks' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
'daily' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf-debug.log',
|
||||
'level' => Logger::DEBUG,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\JsonFormatter::class,
|
||||
'constructor' => [
|
||||
'batchMode' => Formatter\JsonFormatter::BATCH_MODE_JSON,
|
||||
'appendNewline' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
結果如下
|
||||
|
||||
```shell
|
||||
==> runtime/logs/hyperf.log <==
|
||||
[2019-11-08 11:11:35] hyperf.INFO: 5dc4dce791690 [] []
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
## 安裝
|
||||
|
||||
```
|
||||
```shell
|
||||
composer require hyperf/logger
|
||||
```
|
||||
|
||||
@ -305,9 +305,62 @@ return [
|
||||
];
|
||||
```
|
||||
|
||||
結果如下
|
||||
或
|
||||
|
||||
```php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Monolog\Handler;
|
||||
use Monolog\Formatter;
|
||||
use Monolog\Logger;
|
||||
|
||||
return [
|
||||
'default' => [
|
||||
'handlers' => ['single', 'daily'],
|
||||
],
|
||||
|
||||
'single' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Logger::INFO,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\LineFormatter::class,
|
||||
'constructor' => [
|
||||
'format' => null,
|
||||
'dateFormat' => null,
|
||||
'allowInlineLineBreaks' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
'daily' => [
|
||||
'handler' => [
|
||||
'class' => Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf-debug.log',
|
||||
'level' => Logger::DEBUG,
|
||||
],
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Formatter\JsonFormatter::class,
|
||||
'constructor' => [
|
||||
'batchMode' => Formatter\JsonFormatter::BATCH_MODE_JSON,
|
||||
'appendNewline' => true,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
```
|
||||
|
||||
結果如下
|
||||
|
||||
```shell
|
||||
==> runtime/logs/hyperf.log <==
|
||||
[2019-11-08 11:11:35] hyperf.INFO: 5dc4dce791690 [] []
|
||||
|
||||
|
@ -112,6 +112,15 @@ class LoggerFactory
|
||||
$defaultHandlerConfig = $this->getDefaultHandlerConfig($config);
|
||||
$defaultFormatterConfig = $this->getDefaultFormatterConfig($config);
|
||||
foreach ($handlerConfigs as $value) {
|
||||
if (is_string($value)) {
|
||||
if (! $this->config->has($group = 'logger.' . $value)) {
|
||||
continue;
|
||||
}
|
||||
$value = $this->config->get($group . '.handler', []);
|
||||
if ($this->config->has($group . '.formatter')) {
|
||||
$value['formatter'] = $this->config->get($group . '.formatter', []);
|
||||
}
|
||||
}
|
||||
$class = $value['class'] ?? $defaultHandlerConfig['class'];
|
||||
$constructor = $value['constructor'] ?? $defaultHandlerConfig['constructor'];
|
||||
if (isset($value['formatter'])) {
|
||||
|
@ -31,11 +31,7 @@ use ReflectionClass;
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CoversClass(\Hyperf\Logger\LoggerFactory::class)]
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\CoversClass(LoggerFactory::class)]
|
||||
class LoggerFactoryTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
@ -60,6 +56,15 @@ class LoggerFactoryTest extends TestCase
|
||||
$this->assertInstanceOf(\Hyperf\Logger\Logger::class, $logger);
|
||||
}
|
||||
|
||||
public function testInvokeLoggerFromFactoryByString()
|
||||
{
|
||||
$container = $this->mockContainer();
|
||||
ApplicationContext::setContainer($container);
|
||||
$factory = $container->get(LoggerFactory::class);
|
||||
$logger = $factory->get(group: 'string');
|
||||
$this->assertInstanceOf(\Hyperf\Logger\Logger::class, $logger);
|
||||
}
|
||||
|
||||
public function testHandlerConfig()
|
||||
{
|
||||
$container = $this->mockContainer();
|
||||
@ -179,6 +184,7 @@ class LoggerFactoryTest extends TestCase
|
||||
'constructor' => [],
|
||||
],
|
||||
],
|
||||
'string' => ['handlers' => ['default']],
|
||||
'default-handlers' => [
|
||||
'handlers' => [
|
||||
[
|
||||
|
Loading…
Reference in New Issue
Block a user