update
This commit is contained in:
92
src/Library/Logger.php
Normal file
92
src/Library/Logger.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 日志记录
|
||||
*/
|
||||
|
||||
namespace Dhd\Micro\Core\Library;
|
||||
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Throwable;
|
||||
|
||||
class Logger
|
||||
{
|
||||
private string $message = '';
|
||||
private ?array $data = null;
|
||||
private ?Throwable $throwable = null;
|
||||
private string $traceId = '';
|
||||
private array $context = [];
|
||||
private bool $isError = false;
|
||||
|
||||
public function setMessage(string $message): self
|
||||
{
|
||||
$this->message = $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setData(array $data): self
|
||||
{
|
||||
$this->data = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setThrowable(Throwable $throwable): self
|
||||
{
|
||||
$this->throwable = $throwable;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTraceId(string $traceId): self
|
||||
{
|
||||
$this->traceId = $traceId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setContext(array $context): self
|
||||
{
|
||||
$this->context = $context;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isError(): bool
|
||||
{
|
||||
return $this->isError;
|
||||
}
|
||||
|
||||
public function setErrorStatus(bool $isError): self
|
||||
{
|
||||
$this->isError = $isError;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function record(): void
|
||||
{
|
||||
$fatory = ApplicationContext::getContainer()->get(LoggerFactory::class);
|
||||
$logger = $fatory->get('log', 'default');
|
||||
$content = [
|
||||
'message' => $this->message,
|
||||
'trace_id' => $this->traceId,
|
||||
'data' => $this->data,
|
||||
'context' => $this->context,
|
||||
];
|
||||
|
||||
if ($this->throwable !== null) {
|
||||
$throwable = $this->throwable;
|
||||
$content['throwable'] = [
|
||||
'message' => $throwable->getMessage(),
|
||||
'code' => $throwable->getCode(),
|
||||
'file' => $throwable->getFile(),
|
||||
'line' => $throwable->getLine(),
|
||||
'trace' => $this->isError ? $throwable->getTrace() : null,
|
||||
'data' => method_exists($throwable, 'getData') ? $throwable->getData() : null,
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->isError) {
|
||||
$logger->error(json_encode($content, JSON_UNESCAPED_UNICODE));
|
||||
} else {
|
||||
$logger->info(json_encode($content, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Carbon\Carbon;
|
||||
use Hyperf\Logger\LoggerFactory;
|
||||
use Dhd\Micro\Core\Library\Logger;
|
||||
|
||||
final class Util
|
||||
{
|
||||
@@ -24,41 +25,11 @@ final class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录日志信息
|
||||
*
|
||||
* 此方法使用Hyperf的LoggerFactory创建一个日志记录器,并将指定的消息和数据记录到日志中
|
||||
* 日志内容会被格式化为JSON字符串并以info级别记录
|
||||
*
|
||||
* @param string $msg 日志消息内容
|
||||
* @param mixed $data 需要记录的额外数据,可以是任意类型的数据
|
||||
* @param bool $isError 是否为异常日志,默认为false
|
||||
* @return void
|
||||
*
|
||||
* 示例:
|
||||
* Util::recordLog('用户登录', ['user_id' => 123, 'ip' => '192.168.1.1']);
|
||||
* // 这将在日志中记录类似 {"msg":"用户登录","data":{"user_id":123,"ip":"192.168.1.1"}} 的信息
|
||||
* 获取日志记录器
|
||||
*/
|
||||
public static function recordLog(string $msg, mixed $data, string $traceId = '', bool $isError = false): void
|
||||
public static function logger(): Logger
|
||||
{
|
||||
$fatory = self::get(LoggerFactory::class);
|
||||
$logger = $fatory->get('log', 'default');
|
||||
$content = [
|
||||
'msg' => $msg,
|
||||
'trace_id' => $traceId,
|
||||
'data' => ($data instanceof \Throwable) ? [
|
||||
'message' => $data->getMessage(),
|
||||
'code' => $data->getCode(),
|
||||
'file' => $data->getFile(),
|
||||
'line' => $data->getLine(),
|
||||
'trace' => $isError ? $data->getTrace() : null,
|
||||
'data' => method_exists($data, 'getData') ? $data->getData() : null,
|
||||
] : $data,
|
||||
];
|
||||
if ($isError) {
|
||||
$logger->error(json_encode($content, JSON_UNESCAPED_UNICODE));
|
||||
} else {
|
||||
$logger->info(json_encode($content, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
return new Logger();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user