改名
This commit is contained in:
@@ -74,7 +74,6 @@ class BaseObject implements Configure
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
} else {
|
||||
$this->error('set ' . $name . ' not exists ' . static::class);
|
||||
throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Abstracts;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Server\Events\OnAfterRequest;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Logger implements LoggerInterface
|
||||
{
|
||||
|
||||
const EMERGENCY = 'emergency';
|
||||
const ALERT = 'alert';
|
||||
const CRITICAL = 'critical';
|
||||
const ERROR = 'error';
|
||||
const WARNING = 'warning';
|
||||
const NOTICE = 'notice';
|
||||
const INFO = 'info';
|
||||
const DEBUG = 'debug';
|
||||
|
||||
|
||||
/**
|
||||
* @var EventProvider
|
||||
*/
|
||||
#[Inject(EventProvider::class)]
|
||||
public EventProvider $eventProvider;
|
||||
|
||||
private array $_loggers = [];
|
||||
|
||||
|
||||
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
|
||||
|
||||
|
||||
/**
|
||||
* 监听事件
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->eventProvider->on(OnAfterRequest::class, [$this, 'onAfterRequest']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 紧急情况
|
||||
*/
|
||||
public function emergency($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement emergency() method.
|
||||
$this->log(Logger::EMERGENCY, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 应该警惕的
|
||||
*/
|
||||
public function alert($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement alert() method.
|
||||
$this->log(Logger::ALERT, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*
|
||||
* 关键性的日志
|
||||
*/
|
||||
public function critical($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement critical() method.
|
||||
$this->log(Logger::CRITICAL, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function error($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement error() method.
|
||||
$this->log(Logger::ERROR, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function warning($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement warning() method.
|
||||
$this->log(Logger::WARNING, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function notice($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement notice() method.
|
||||
$this->log(Logger::NOTICE, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function info($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement info() method.
|
||||
$this->log(Logger::INFO, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function debug($message, array $context = array())
|
||||
{
|
||||
// TODO: Implement debug() method.
|
||||
$this->log(Logger::DEBUG, $message, $context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
private function log($level, $message, array $context = array())
|
||||
{
|
||||
// TODO: Implement log() method.
|
||||
$levels = Config::get('log.level', Logger::LOGGER_LEVELS);
|
||||
if (!in_array($level, $levels)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$_string = '[' . now() . '] production.' . $level . ': ' . $this->_string($message, $context);
|
||||
|
||||
$this->_loggers[] = $_string;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param OnAfterRequest $afterRequest
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onAfterRequest(OnAfterRequest $afterRequest)
|
||||
{
|
||||
$loggers = implode(PHP_EOL, $this->_loggers);
|
||||
$this->_loggers = [];
|
||||
|
||||
$filename = storage('log-' . date('Y-m-d') . '.log', 'logs/');
|
||||
|
||||
file_put_contents($filename, $loggers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $context
|
||||
* @return string
|
||||
*/
|
||||
private function _string($message, $context): string
|
||||
{
|
||||
return $message . ' ' . print_r($context, true) . PHP_EOL;
|
||||
}
|
||||
}
|
||||
@@ -213,7 +213,7 @@ class Application extends BaseApplication
|
||||
|
||||
$data = $this->getBuilder($manager->exec($class));
|
||||
} catch (\Throwable $exception) {
|
||||
$data = $this->getBuilder(logger()->exception($exception));
|
||||
$data = $this->getBuilder(jTraceEx($exception));
|
||||
} finally {
|
||||
print_r($data);
|
||||
Timer::clearAll();
|
||||
|
||||
@@ -11,10 +11,12 @@ namespace Kiri\Di;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Server;
|
||||
use Kiri\Abstracts\BaseObject;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\Test\TestLogger;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionFunction;
|
||||
@@ -63,7 +65,8 @@ class Container extends BaseObject implements ContainerInterface
|
||||
/** @var array|string[] */
|
||||
private array $_interfaces = [
|
||||
RequestInterface::class => Request::class,
|
||||
ResponseInterface::class => Response::class
|
||||
ResponseInterface::class => Response::class,
|
||||
LoggerInterface::class => Logger::class
|
||||
];
|
||||
|
||||
|
||||
|
||||
+14
-175
@@ -17,13 +17,14 @@ use Kiri\Core\Json;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Swoole\Coroutine;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class Logger
|
||||
* @package Kiri\Kiri\Error
|
||||
* @mixin \Kiri\Abstracts\Logger
|
||||
*/
|
||||
class Logger extends Component
|
||||
{
|
||||
@@ -36,136 +37,25 @@ class Logger extends Component
|
||||
public EventProvider $eventProvider;
|
||||
|
||||
|
||||
/**
|
||||
* inject logger
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
#[Inject(LoggerInterface::class)]
|
||||
public LoggerInterface $logger;
|
||||
|
||||
|
||||
private array $sources = [];
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->eventProvider->on(OnAfterRequest::class, [$this, 'insert']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function debug(mixed $message, string $method = 'app', $file = null)
|
||||
{
|
||||
if (Config::get('environment', 'pro') == 'pro') {
|
||||
return;
|
||||
}
|
||||
$this->output($message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trance(mixed $message, string $method = 'app')
|
||||
{
|
||||
if (Config::get('environment', 'pro') == 'pro') {
|
||||
return;
|
||||
}
|
||||
$this->output($message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function error(mixed $message, $method = 'error', $file = null)
|
||||
{
|
||||
$this->writer($message, $method);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $message
|
||||
* @param string $method
|
||||
* @param null $file
|
||||
* @throws Exception
|
||||
*/
|
||||
public function success(mixed $message, string $method = 'app', $file = null)
|
||||
{
|
||||
if (Config::get('environment', 'pro') == 'pro') {
|
||||
return;
|
||||
}
|
||||
$this->output($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $method
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function writer($message, string $method = 'app'): void
|
||||
{
|
||||
if (empty($message)) {
|
||||
return;
|
||||
}
|
||||
$message = print_r($message, true);
|
||||
$this->print_r($message, $method);
|
||||
if (!is_array($this->logs)) {
|
||||
$this->logs = [];
|
||||
}
|
||||
$this->logs[$method] = $message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $method
|
||||
* @throws Exception
|
||||
*/
|
||||
public function print_r($message, string $method = '')
|
||||
{
|
||||
$debug = Config::get('debug', ['enable' => false]);
|
||||
if ((bool)$debug['enable'] === true) {
|
||||
if (!is_callable($debug['callback'] ?? null, true)) {
|
||||
return;
|
||||
}
|
||||
call_user_func($debug['callback'], $message, $method);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $method
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function output($message, string $method = 'default')
|
||||
{
|
||||
if (Config::get('environment', 'dev') == 'pro') {
|
||||
if ($method === 'error') {
|
||||
echo $message;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (str_contains($message, 'Event::rshutdown(): Event::wait()')) {
|
||||
return;
|
||||
}
|
||||
echo $message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $application
|
||||
* @return mixed
|
||||
* @return string
|
||||
*/
|
||||
public function getLastError(string $application = 'app'): mixed
|
||||
public function getLastError(string $application = 'app'): string
|
||||
{
|
||||
return $this->logs[$application] ?? 'Unknown error.';
|
||||
return 'Unknown error.';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,21 +77,6 @@ class Logger extends Component
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $logFile
|
||||
* @return string
|
||||
*/
|
||||
private function getSource($logFile): string
|
||||
{
|
||||
if (!file_exists($logFile)) {
|
||||
Coroutine\System::exec('echo 3 > /proc/sys/vm/drop_caches');
|
||||
touch($logFile);
|
||||
}
|
||||
if (is_writeable($logFile)) {
|
||||
$logFile = realpath($logFile);
|
||||
}
|
||||
return $logFile;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -219,31 +94,6 @@ class Logger extends Component
|
||||
$this->logs = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function clear(): array
|
||||
{
|
||||
return $this->logs = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function arrayFormat($data): string
|
||||
{
|
||||
if (is_string($data)) {
|
||||
return $data;
|
||||
}
|
||||
if ($data instanceof Throwable) {
|
||||
$data = $this->getException($data);
|
||||
} else if (is_object($data)) {
|
||||
$data = get_object_vars($data);
|
||||
}
|
||||
return Json::encode($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Throwable $exception
|
||||
@@ -264,16 +114,5 @@ class Logger extends Component
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Throwable $exception
|
||||
* @return array
|
||||
*/
|
||||
private function getException(Throwable $exception): array
|
||||
{
|
||||
$filetype = [$exception->getMessage()];
|
||||
$filetype[] = $exception->getFile() . ' on line ' . $exception->getLine();
|
||||
$filetype[] = $exception->getTrace();
|
||||
return $filetype;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+17
-3
@@ -10,8 +10,8 @@ use Http\Context\Response as HttpResponse;
|
||||
use Http\Route\Router;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\AspectManager;
|
||||
use Kiri\Application;
|
||||
use Kiri\AspectManager;
|
||||
use Kiri\Core\ArrayAccess;
|
||||
use Kiri\Error\Logger;
|
||||
use Kiri\Events\EventDispatch;
|
||||
@@ -19,6 +19,7 @@ use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Server\Constrict\Request;
|
||||
use Server\Constrict\Response;
|
||||
use Swoole\WebSocket\Server;
|
||||
@@ -54,6 +55,19 @@ if (!function_exists('make')) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('now')) {
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
function now(): string
|
||||
{
|
||||
return date('Y-m-d H:i:s') . '.' . str_replace('0.', '', (string)microtime(true));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('workerName')) {
|
||||
|
||||
|
||||
@@ -751,7 +765,7 @@ if (!function_exists('event')) {
|
||||
function event($name, $callback, bool $isAppend = true)
|
||||
{
|
||||
$pro = di(EventProvider::class);
|
||||
$pro->on($name, $callback,0);
|
||||
$pro->on($name, $callback, 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1119,7 +1133,7 @@ if (!function_exists('error')) {
|
||||
*/
|
||||
function error(mixed $message, string $method = 'error')
|
||||
{
|
||||
Kiri::app()->error($message, $method);
|
||||
Kiri::getDi()->get(LoggerInterface::class)->error($method, [$message]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use Annotation\Inject;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Application;
|
||||
use Kiri\Di\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Server\RequestInterface;
|
||||
use Server\ResponseInterface;
|
||||
|
||||
@@ -55,4 +56,14 @@ class Controller
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* inject logger
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
#[Inject(LoggerInterface::class)]
|
||||
public LoggerInterface $logger;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ abstract class Http extends Server implements OnRequest
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
|
||||
@@ -4,10 +4,13 @@
|
||||
namespace Server\Abstracts;
|
||||
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
||||
/**
|
||||
@@ -18,6 +21,13 @@ abstract class Server
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
#[Inject(LoggerInterface::class)]
|
||||
public LoggerInterface $logger;
|
||||
|
||||
|
||||
/**
|
||||
* @param $prefix
|
||||
* @throws ConfigException
|
||||
|
||||
Reference in New Issue
Block a user