144 lines
3.9 KiB
PHP
144 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Lib;
|
|
|
|
use Hyperf\Context\ApplicationContext;
|
|
use Hyperf\Logger\LoggerFactory;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class Log
|
|
{
|
|
/**
|
|
* 获取日志类
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return LoggerInterface
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
private function getLogger(string $name = 'app',string $group = 'app'): LoggerInterface
|
|
{
|
|
return ApplicationContext::getContainer()->get(LoggerFactory::class)->get($name, $group);
|
|
}
|
|
|
|
/**
|
|
* info级别日志
|
|
* @param $msg
|
|
* @param array $content
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function info($msg, array $content = [],string $name = 'info',string $group = 'app'): void
|
|
{
|
|
$this->getLogger($name,$group)->info($msg,$content);
|
|
}
|
|
|
|
/**
|
|
* debug级别日志
|
|
* @param $msg
|
|
* @param array $content
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function debug($msg, array $content = [],string $name = 'debug',string $group = 'error'): void
|
|
{
|
|
$this->getLogger($name,$group)->debug($msg,$content);
|
|
}
|
|
|
|
/**
|
|
* notice级别的日志
|
|
* @param $msg
|
|
* @param array $content
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function notice($msg, array $content = [],string $name = 'notice',string $group = 'app'): void
|
|
{
|
|
$this->getLogger($name,$group)->notice($msg,$content);
|
|
}
|
|
|
|
/**
|
|
* error级别的日志
|
|
* @param $msg
|
|
* @param array $content
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function error($msg, array $content = [],string $name = 'error',string $group = 'error'): void
|
|
{
|
|
$this->getLogger($name,$group)->error($msg,$content);
|
|
}
|
|
|
|
/**
|
|
* callback 请求日志
|
|
* @param $msg
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function callbackLog($msg, string $name = 'info', string $group = 'callback'): void
|
|
{
|
|
$this->getLogger($name,$group)->info($msg);
|
|
}
|
|
|
|
/**
|
|
* admin 请求日志
|
|
* @param $msg
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function requestAdminLog($msg, string $name = 'info', string $group = 'request-admin'): void
|
|
{
|
|
$this->getLogger($name,$group)->info($msg);
|
|
}
|
|
|
|
/**
|
|
* api 请求日志
|
|
* @param $msg
|
|
* @param string $name
|
|
* @param string $group
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function requestApiLog($msg, string $name = 'info', string $group = 'request-api'): void
|
|
{
|
|
$this->getLogger($name,$group)->info($msg);
|
|
}
|
|
|
|
|
|
/**
|
|
* 用户级别的日志
|
|
* @param int $userId
|
|
* @param string $msg
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
function userLog(int $userId, string $msg = ''): void
|
|
{
|
|
$traces = debug_backtrace();
|
|
$class = $traces[1] ? $traces[1]['class'] : '';
|
|
$func = $traces[1] ? $traces[1]['function'] : '';
|
|
$this->info("$class::$func==$userId==" . (is_string($msg) ? $msg : json_encode($msg)));
|
|
}
|
|
} |