Files
wh-api/app/Log/Log.php
ykxiao 0b2299c427
Some checks failed
Build Docker / build (push) Has been cancelled
协程版仓库后端项目
2025-07-08 14:59:47 +08:00

62 lines
1.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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());
}
}
}