52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
||
|
||
namespace App\Utils;
|
||
|
||
use Hyperf\Logger\LoggerFactory;
|
||
use Hyperf\Context\ApplicationContext;
|
||
use Psr\Log\LoggerInterface;
|
||
|
||
class Log
|
||
{
|
||
/**
|
||
* 获取指定频道的日志实例
|
||
*
|
||
* @param string $channel 日志频道名称
|
||
* @return LoggerInterface
|
||
*/
|
||
public static function get(string $channel = 'app'): LoggerInterface
|
||
{
|
||
return ApplicationContext::getContainer()->get(LoggerFactory::class)->get($channel);
|
||
}
|
||
|
||
/**
|
||
* 选择日志频道(链式调用)
|
||
*
|
||
* @param string $channel 日志频道名称
|
||
* @return LoggerInterface
|
||
*
|
||
* @example Log::channel('custom')->error('Error message')
|
||
* @example Log::channel('api')->info('API request', ['url' => '/api/users'])
|
||
* @example Log::channel('database')->debug('Query executed', ['sql' => 'SELECT * FROM users'])
|
||
*/
|
||
public static function channel(string $channel): LoggerInterface
|
||
{
|
||
return self::get($channel);
|
||
}
|
||
|
||
/**
|
||
* 魔术方法:静态调用日志方法(使用默认频道 'app')
|
||
* 支持的日志级别:emergency, alert, critical, error, warning, notice, info, debug
|
||
*
|
||
* @param string $method 方法名(日志级别)
|
||
* @param array $args 参数数组
|
||
* @return void
|
||
*
|
||
* @example Log::error('Error message', ['context' => 'data'])
|
||
* @example Log::info('Info message')
|
||
*/
|
||
public static function __callStatic(string $method, array $args): void
|
||
{
|
||
self::get()->$method(...$args);
|
||
}
|
||
} |