62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?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());
|
||
}
|
||
}
|
||
} |