Added a logger component for hyperf.

This commit is contained in:
李铭昕 2019-03-05 17:47:37 +08:00
parent bf7e8cc4c8
commit 9c9471702f
6 changed files with 217 additions and 3 deletions

View File

@ -34,7 +34,8 @@
"php-amqplib/php-amqplib": "^2.7",
"openzipkin/zipkin": "^1.3.2",
"grpc/grpc": "^1.15",
"elasticsearch/elasticsearch": "^6.1"
"elasticsearch/elasticsearch": "^6.1",
"monolog/monolog": "^1.24"
},
"require-dev": {
"malukenho/docheader": "^0.1.6",
@ -58,11 +59,12 @@
"hyperf/dispatcher": "self.version",
"hyperf/elasticsearch": "self.version",
"hyperf/event": "self.version",
"hyperf/framework": "self.version",
"hyperf/grpc-client": "self.version",
"hyperf/grpc-server": "self.version",
"hyperf/guzzle": "self.version",
"hyperf/http-server": "self.version",
"hyperf/framework": "self.version",
"hyperf/logger": "self.version",
"hyperf/memory": "self.version",
"hyperf/model-cache": "self.version",
"hyperf/paginator": "self.version",
@ -95,11 +97,12 @@
"Hyperf\\Dispatcher\\": "src/dispatcher/src/",
"Hyperf\\Elasticsearch\\": "src/elasticsearch/src/",
"Hyperf\\Event\\": "src/event/src/",
"Hyperf\\Framework\\": "src/framework/src/",
"Hyperf\\GrpcClient\\": "src/grpc-client/src/",
"Hyperf\\GrpcServer\\": "src/grpc-server/src/",
"Hyperf\\Guzzle\\": "src/guzzle/src/",
"Hyperf\\HttpServer\\": "src/http-server/src/",
"Hyperf\\Framework\\": "src/framework/src/",
"Hyperf\\Logger\\": "src/logger/src/",
"Hyperf\\Memory\\": "src/memory/src/",
"Hyperf\\ModelCache\\": "src/model-cache/src/",
"Hyperf\\Paginator\\": "src/paginator/src/",
@ -142,6 +145,7 @@
"Hyperf\\GrpcClient\\ConfigProvider",
"Hyperf\\GrpcServer\\ConfigProvider",
"Hyperf\\HttpServer\\ConfigProvider",
"Hyperf\\Logger\\ConfigProvider",
"Hyperf\\Memory\\ConfigProvider",
"Hyperf\\ModelCache\\ConfigProvider",
"Hyperf\\Paginator\\ConfigProvider",

52
src/logger/composer.json Normal file
View File

@ -0,0 +1,52 @@
{
"name": "hyperf/logger",
"description": "A logger component for hyperf.",
"license": "Apache-2.0",
"keywords": [
"php",
"hyperf",
"logger"
],
"support": {
},
"require": {
"php": ">=7.2",
"psr/log": "^1.0",
"monolog/monolog": "^1.24"
},
"require-dev": {
"malukenho/docheader": "^0.1.6",
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^7.0.0",
"friendsofphp/php-cs-fixer": "^2.9"
},
"suggest": {
},
"autoload": {
"files": [
],
"psr-4": {
"Hyperf\\Logger\\": "src"
}
},
"autoload-dev": {
"psr-4": {
}
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
},
"hyperf": {
"config": "Hyperf\\Logger\\ConfigProvider"
}
},
"bin": [
],
"scripts": {
"cs-fix": "php-cs-fixer fix $1",
"test": "phpunit --colors=always"
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Logger;
class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => [
],
'commands' => [
],
'scan' => [
'paths' => [
__DIR__,
],
],
];
}
}

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Logger\Exceptions;
class InvalidConfigException extends \RuntimeException
{
}

20
src/logger/src/Logger.php Normal file
View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Logger;
use Monolog\Logger as MonoLogger;
use Hyperf\Contract\StdoutLoggerInterface;
class Logger extends MonoLogger implements StdoutLoggerInterface
{
}

View File

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Logger;
use Psr\Log\LoggerInterface;
use Hyperf\Contract\ConfigInterface;
use Monolog\Handler\HandlerInterface;
use Psr\Container\ContainerInterface;
use Monolog\Formatter\FormatterInterface;
use Hyperf\Logger\Exceptions\InvalidConfigException;
class LoggerFactory
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var ConfigInterface
*/
protected $config;
/**
* @var array
*/
protected $loggers;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->config = $container->get(ConfigInterface::class);
}
public function make($name = 'default', $subname = null): LoggerInterface
{
$config = $this->config->get('logger');
if (! isset($config[$name])) {
throw new InvalidConfigException(sprintf('Logger config[%s] is not defined.', $name));
}
$config = $config[$name];
$handler = $this->handler($config);
$subname && $name = $name . '.' . $subname;
return make(Logger::class, [
'name' => $name,
'handlers' => [$handler],
]);
}
public function get($name = 'default'): LoggerInterface
{
if (isset($this->loggers[$name]) && $this->loggers[$name] instanceof Logger) {
return $this->loggers[$name];
}
return $this->loggers[$name] = $this->make($name);
}
protected function handler(array $config): HandlerInterface
{
$handlerClass = $config['handler']['class'];
$handlerConstructor = $config['handler']['constructor'];
/** @var HandlerInterface $handler */
$handler = make($handlerClass, $handlerConstructor);
$formatterClass = $config['formatter']['class'];
$formatterConstructor = $config['formatter']['constructor'];
/** @var FormatterInterface $formatter */
$formatter = make($formatterClass, $formatterConstructor);
$handler->setFormatter($formatter);
return $handler;
}
}