协程版仓库后端项目
Some checks failed
Build Docker / build (push) Has been cancelled

This commit is contained in:
2025-07-08 14:59:47 +08:00
commit 0b2299c427
134 changed files with 19277 additions and 0 deletions

46
app/Log/AliSlsHandler.php Normal file
View File

@ -0,0 +1,46 @@
<?php
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午6:33
* Description:
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
declare(strict_types=1);
namespace App\Log;
use App\Amqp\Producer\AliSlsProducer;
use Hyperf\Amqp\Producer;
use Hyperf\Di\Annotation\Inject;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午7:19
* Description: 阿里云日志服务处理器.
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
class AliSlsHandler extends AbstractProcessingHandler
{
#[Inject]
protected Producer $producer;
protected function write(LogRecord $record): void
{
$logs = ['channel' => $record['channel'] ?? '', 'formatted' => $record['formatted'] ?? ''];
$message = new AliSlsProducer($logs);
$this->producer->produce($message, true);
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午6:33
* Description:
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
declare(strict_types=1);
namespace App\Log;
use Hyperf\Context\Context;
use Hyperf\Coroutine\Coroutine;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午7:19
* Description: 添加请求ID和协程ID
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
class AppendRequestIdProcessor implements ProcessorInterface
{
public const string REQUEST_ID = 'log.request.id';
public function __invoke(LogRecord $record): array|LogRecord
{
$record['extra']['request_id'] = Context::getOrSet(self::REQUEST_ID, uniqid('xw_cloud'));
$record['extra']['coroutine_id'] = Coroutine::id();
return $record;
}
}

62
app/Log/Log.php Normal file
View File

@ -0,0 +1,62 @@
<?php
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午6:42
* Description:
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
declare(strict_types=1);
namespace App\Log;
use App\Exception\ApiException;
use Hyperf\Context\ApplicationContext;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use function Hyperf\Support\make;
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午7:19
* Description: 日志记录器类。
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
class Log
{
#[Inject]
protected LoggerInterface $logger;
/**
* 根据提供的名称和分组获取日志记录器实例。
*
* @param string $name 日志记录器的名称,默认为'app'。
* @param string $group 日志记录器的分组,默认为'job'。
* @return LoggerInterface 返回一个日志记录器实例。
*/
public static function get(string $name = 'app', string $group = 'job'): LoggerInterface
{
try {
// 尝试从应用上下文容器中获取LoggerFactory实例并进一步获取指定名称和分组的日志记录器
return ApplicationContext::getContainer()->get(LoggerFactory::class)->get($name, $group);
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
// 如果在获取过程中发生异常使用默认的日志记录器记录错误信息并抛出ApiException
$logs = make(LoggerFactory::class)->get('default');
$logs->error($e->getMessage());
throw new ApiException($e->getMessage());
}
}
}

View File

@ -0,0 +1,48 @@
<?php
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午6:03
* Description:
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
declare(strict_types=1);
namespace App\Log;
use Hyperf\Context\ApplicationContext;
use Hyperf\Logger\LoggerFactory;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午6:04
* Description: 创建一个日志记录器
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
class StdoutLoggerFactory
{
/**
* @param ContainerInterface $container
* @return LoggerInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container): LoggerInterface
{
return ApplicationContext::getContainer()->get(LoggerFactory::class)->get();
}
}