mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-30 02:37:58 +08:00
Added multi handlers for logger.
This commit is contained in:
parent
63f2e857cf
commit
0a9f7d62e8
@ -3,6 +3,7 @@
|
||||
## Added
|
||||
|
||||
- [#405](https://github.com/hyperf-cloud/hyperf/pull/405) Added Context::override() method.
|
||||
- [#409](https://github.com/hyperf-cloud/hyperf/pull/409) Added multi handlers for logger.
|
||||
|
||||
# v1.0.11 - 2019-08-15
|
||||
|
||||
|
@ -12,15 +12,30 @@ declare(strict_types=1);
|
||||
|
||||
return [
|
||||
'default' => [
|
||||
'handler' => [
|
||||
'class' => \Monolog\Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => \Monolog\Logger::DEBUG,
|
||||
'handlers' => [
|
||||
[
|
||||
'class' => Monolog\Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Monolog\Logger::DEBUG,
|
||||
],
|
||||
'formatter' => [
|
||||
'class' => Monolog\Formatter\LineFormatter::class,
|
||||
'constructor' => [],
|
||||
],
|
||||
],
|
||||
],
|
||||
// Default Handler
|
||||
'handler' => [
|
||||
'class' => Monolog\Handler\StreamHandler::class,
|
||||
'constructor' => [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Monolog\Logger::DEBUG,
|
||||
],
|
||||
],
|
||||
// Default Formatter
|
||||
'formatter' => [
|
||||
'class' => \Monolog\Formatter\LineFormatter::class,
|
||||
'class' => Monolog\Formatter\LineFormatter::class,
|
||||
'constructor' => [],
|
||||
],
|
||||
],
|
||||
|
@ -14,8 +14,11 @@ namespace Hyperf\Logger;
|
||||
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Logger\Exception\InvalidConfigException;
|
||||
use Hyperf\Utils\Arr;
|
||||
use Monolog\Formatter\FormatterInterface;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
use Monolog\Handler\HandlerInterface;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
@ -50,11 +53,11 @@ class LoggerFactory
|
||||
}
|
||||
|
||||
$config = $config[$group];
|
||||
$handler = $this->handler($config);
|
||||
$handlers = $this->handlers($config);
|
||||
|
||||
return make(Logger::class, [
|
||||
'name' => $name,
|
||||
'handlers' => [$handler],
|
||||
'handlers' => $handlers,
|
||||
]);
|
||||
}
|
||||
|
||||
@ -67,16 +70,55 @@ class LoggerFactory
|
||||
return $this->loggers[$name] = $this->make($name, $group);
|
||||
}
|
||||
|
||||
protected function handler(array $config): HandlerInterface
|
||||
protected function getDefaultFormatterConfig($config)
|
||||
{
|
||||
$handlerClass = $config['handler']['class'];
|
||||
$handlerConstructor = $config['handler']['constructor'];
|
||||
$formatterClass = Arr::get($config, 'formatter.class', LineFormatter::class);
|
||||
$formatterConstructor = Arr::get($config, 'formatter.constructor', []);
|
||||
|
||||
return [
|
||||
'class' => $formatterClass,
|
||||
'constructor' => $formatterConstructor,
|
||||
];
|
||||
}
|
||||
|
||||
protected function getDefaultHandlerConfig($config)
|
||||
{
|
||||
$handlerClass = Arr::get($config, 'handler.class', StreamHandler::class);
|
||||
$handlerConstructor = Arr::get($config, 'handler.constructor', [
|
||||
'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
|
||||
'level' => Logger::DEBUG,
|
||||
]);
|
||||
|
||||
return [
|
||||
'class' => $handlerClass,
|
||||
'constructor' => $handlerConstructor,
|
||||
];
|
||||
}
|
||||
|
||||
protected function handlers(array $config): array
|
||||
{
|
||||
$handlerConfigs = $config['handlers'] ?? [[]];
|
||||
$handlers = [];
|
||||
$defaultHandlerConfig = $this->getDefaultHandlerConfig($config);
|
||||
$defaultFormatterConfig = $this->getDefaultFormatterConfig($config);
|
||||
foreach ($handlerConfigs as $value) {
|
||||
$class = $value['class'] ?? $defaultHandlerConfig['class'];
|
||||
$constructor = $value['constructor'] ?? $defaultHandlerConfig['constructor'];
|
||||
$formatterConfig = $value['formatter'] ?? $defaultFormatterConfig;
|
||||
|
||||
$handlers[] = $this->handler($class, $constructor, $formatterConfig);
|
||||
}
|
||||
|
||||
return $handlers;
|
||||
}
|
||||
|
||||
protected function handler($class, $constructor, $formatterConfig): HandlerInterface
|
||||
{
|
||||
/** @var HandlerInterface $handler */
|
||||
$handler = make($handlerClass, $handlerConstructor);
|
||||
$handler = make($class, $constructor);
|
||||
|
||||
$formatterClass = $config['formatter']['class'];
|
||||
$formatterConstructor = $config['formatter']['constructor'];
|
||||
$formatterClass = $formatterConfig['class'];
|
||||
$formatterConstructor = $formatterConfig['constructor'];
|
||||
|
||||
/** @var FormatterInterface $formatter */
|
||||
$formatter = make($formatterClass, $formatterConstructor);
|
||||
|
@ -57,7 +57,7 @@ class LoggerFactoryTest extends TestCase
|
||||
{
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
|
||||
$container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config([
|
||||
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
|
||||
'logger' => [
|
||||
'default' => [
|
||||
'handler' => [
|
||||
@ -76,7 +76,6 @@ class LoggerFactoryTest extends TestCase
|
||||
]));
|
||||
|
||||
$container->shouldReceive('get')
|
||||
->once()
|
||||
->with(LoggerFactory::class)
|
||||
->andReturn(new LoggerFactory($container));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user