Logger支持通道与级别,LogHelper统一委托Logger
This commit is contained in:
@@ -4,13 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Dhd\Micro\Core\Library;
|
||||
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Hyperf\Rpc\Context as RpcContext;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
|
||||
/**
|
||||
* 统一日志静态入口,支持 trace_id 注入与多通道.
|
||||
* 统一日志静态入口,内部委托核心 Logger 实现,支持 trace_id 注入与多通道.
|
||||
*/
|
||||
class LogHelper
|
||||
{
|
||||
@@ -47,34 +45,21 @@ class LogHelper
|
||||
|
||||
protected static function log(string $level, string $message, array $context = [], string $channel = 'default'): void
|
||||
{
|
||||
$logger = self::getLogger($channel);
|
||||
$enrichedContext = array_merge($context, self::getTraceContext());
|
||||
$logger->log($level, $message, $enrichedContext);
|
||||
Util::logger()
|
||||
->setChannel($channel)
|
||||
->setLevel($level)
|
||||
->setMessage($message)
|
||||
->setContext($context)
|
||||
->setTraceId(self::getTraceId())
|
||||
->record();
|
||||
}
|
||||
|
||||
protected static function getLogger(string $channel = 'default'): LoggerInterface
|
||||
protected static function getTraceId(): ?string
|
||||
{
|
||||
$container = ApplicationContext::getContainer();
|
||||
$loggerFactory = $container->get(LoggerFactory::class);
|
||||
return $loggerFactory->get($channel);
|
||||
}
|
||||
|
||||
protected static function getTraceContext(): array
|
||||
{
|
||||
$context = [];
|
||||
|
||||
try {
|
||||
$container = ApplicationContext::getContainer();
|
||||
$rpcContext = $container->get(RpcContext::class);
|
||||
|
||||
$traceId = $rpcContext->get('trace_id');
|
||||
if ($traceId) {
|
||||
$context['trace_id'] = $traceId;
|
||||
}
|
||||
return ApplicationContext::getContainer()->get(RpcContext::class)->get('trace_id');
|
||||
} catch (Throwable $e) {
|
||||
// 上下文不可用时静默降级
|
||||
return null;
|
||||
}
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ class Logger
|
||||
private ?string $traceId = '';
|
||||
private array $context = [];
|
||||
private bool $isError = false;
|
||||
private string $channel = 'default';
|
||||
private ?string $level = null;
|
||||
|
||||
public function setMessage(string $message): static
|
||||
{
|
||||
@@ -62,10 +64,28 @@ class Logger
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置日志通道(对应 logger.php 的 group,如 default/error/business/sql).
|
||||
*/
|
||||
public function setChannel(string $channel): static
|
||||
{
|
||||
$this->channel = $channel;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置日志级别(debug/info/warning/error).
|
||||
*/
|
||||
public function setLevel(?string $level): static
|
||||
{
|
||||
$this->level = $level;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function record(): void
|
||||
{
|
||||
$fatory = ApplicationContext::getContainer()->get(LoggerFactory::class);
|
||||
$logger = $fatory->get('log', 'default');
|
||||
$logger = $fatory->get('log', $this->channel);
|
||||
$content = [
|
||||
'message' => $this->message,
|
||||
'timestamp' => date('Y-m-d H:i:s'),
|
||||
@@ -86,10 +106,7 @@ class Logger
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->isError) {
|
||||
$logger->error(json_encode($content, JSON_UNESCAPED_UNICODE));
|
||||
} else {
|
||||
$logger->info(json_encode($content, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
$level = $this->level ?? ($this->isError ? 'error' : 'info');
|
||||
$logger->log($level, json_encode($content, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user