e
This commit is contained in:
+158
-158
@@ -1,158 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/26 0026
|
||||
* Time: 10:00
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
use Exception;
|
||||
use Http\Handler\Formatter\IFormatter;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Kiri;
|
||||
use Http\Events\OnAfterRequest;
|
||||
|
||||
/**
|
||||
* Class ErrorHandler
|
||||
*
|
||||
* @package Kiri\Kiri\Base
|
||||
* @property-read $asError
|
||||
*/
|
||||
class ErrorHandler extends Component implements ErrorInterface
|
||||
{
|
||||
|
||||
/** @var ?IFormatter $message */
|
||||
private ?IFormatter $message = NULL;
|
||||
|
||||
public string $category = 'app';
|
||||
|
||||
/**
|
||||
* 错误处理注册
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
// ini_set('display_errors', '1');
|
||||
set_exception_handler([$this, 'exceptionHandler']);
|
||||
if (defined('HHVM_VERSION')) {
|
||||
set_error_handler([$this, 'errorHandler']);
|
||||
} else {
|
||||
set_error_handler([$this, 'errorHandler']);
|
||||
}
|
||||
register_shutdown_function([$this, 'shutdown']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function shutdown()
|
||||
{
|
||||
$lastError = error_get_last();
|
||||
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->category = 'shutdown';
|
||||
|
||||
$messages = explode(PHP_EOL, $lastError['message']);
|
||||
|
||||
$message = array_shift($messages);
|
||||
|
||||
$this->sendError($message, $lastError['file'], $lastError['line']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Throwable $exception
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exceptionHandler(\Throwable $exception)
|
||||
{
|
||||
$this->category = 'exception';
|
||||
|
||||
di(EventDispatch::class)->dispatch(new OnAfterRequest());
|
||||
|
||||
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
* 以异常形式抛出错误,防止执行后续程序
|
||||
*/
|
||||
public function errorHandler()
|
||||
{
|
||||
$error = func_get_args();
|
||||
|
||||
$path = ['file' => $error[2], 'line' => $error[3]];
|
||||
|
||||
if ($error[0] === 0) {
|
||||
$error[0] = 500;
|
||||
}
|
||||
|
||||
$data = Json::to(500, $error[1], $path);
|
||||
|
||||
Kiri::app()->error($data, 'error');
|
||||
|
||||
di(EventDispatch::class)->dispatch(new OnAfterRequest());
|
||||
|
||||
|
||||
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param int $code
|
||||
* @return false|string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendError($message, $file, $line, $code = 500): bool|string
|
||||
{
|
||||
$path = ['file' => $file, 'line' => $line];
|
||||
|
||||
var_dump(func_get_args());
|
||||
|
||||
$data = Json::to($code, $this->category . ': ' . $message, $path);
|
||||
|
||||
write($data, $this->category);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getErrorMessage(): mixed
|
||||
{
|
||||
$message = $this->message;
|
||||
$this->message = NULL;
|
||||
return $message->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getAsError(): bool
|
||||
{
|
||||
return $this->message !== NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function writer($message, string $category = 'app')
|
||||
{
|
||||
Kiri::app()->debug($message, $category);
|
||||
}
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/26 0026
|
||||
* Time: 10:00
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
use Exception;
|
||||
use Http\Handler\Formatter\IFormatter;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Kiri;
|
||||
use Http\Events\OnAfterRequest;
|
||||
|
||||
/**
|
||||
* Class ErrorHandler
|
||||
*
|
||||
* @package Kiri\Kiri\Base
|
||||
* @property-read $asError
|
||||
*/
|
||||
class ErrorHandler extends Component implements ErrorInterface
|
||||
{
|
||||
|
||||
/** @var ?IFormatter $message */
|
||||
private ?IFormatter $message = NULL;
|
||||
|
||||
public string $category = 'app';
|
||||
|
||||
/**
|
||||
* 错误处理注册
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
// ini_set('display_errors', '1');
|
||||
set_exception_handler([$this, 'exceptionHandler']);
|
||||
if (defined('HHVM_VERSION')) {
|
||||
set_error_handler([$this, 'errorHandler']);
|
||||
} else {
|
||||
set_error_handler([$this, 'errorHandler']);
|
||||
}
|
||||
register_shutdown_function([$this, 'shutdown']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function shutdown()
|
||||
{
|
||||
$lastError = error_get_last();
|
||||
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->category = 'shutdown';
|
||||
|
||||
$messages = explode(PHP_EOL, $lastError['message']);
|
||||
|
||||
$message = array_shift($messages);
|
||||
|
||||
$this->sendError($message, $lastError['file'], $lastError['line']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Throwable $exception
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exceptionHandler(\Throwable $exception)
|
||||
{
|
||||
$this->category = 'exception';
|
||||
|
||||
di(EventDispatch::class)->dispatch(new OnAfterRequest());
|
||||
|
||||
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
* 以异常形式抛出错误,防止执行后续程序
|
||||
*/
|
||||
public function errorHandler()
|
||||
{
|
||||
$error = func_get_args();
|
||||
|
||||
$path = ['file' => $error[2], 'line' => $error[3]];
|
||||
|
||||
if ($error[0] === 0) {
|
||||
$error[0] = 500;
|
||||
}
|
||||
|
||||
$data = Json::to(500, $error[1], $path);
|
||||
|
||||
Kiri::app()->error($data, 'error');
|
||||
|
||||
di(EventDispatch::class)->dispatch(new OnAfterRequest());
|
||||
|
||||
|
||||
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param int $code
|
||||
* @return false|string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sendError($message, $file, $line, $code = 500): bool|string
|
||||
{
|
||||
$path = ['file' => $file, 'line' => $line];
|
||||
|
||||
var_dump(func_get_args());
|
||||
|
||||
$data = Json::to($code, $this->category . ': ' . $message, $path);
|
||||
|
||||
write($data, $this->category);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getErrorMessage(): mixed
|
||||
{
|
||||
$message = $this->message;
|
||||
$this->message = NULL;
|
||||
return $message->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getAsError(): bool
|
||||
{
|
||||
return $this->message !== NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function writer($message, string $category = 'app')
|
||||
{
|
||||
Kiri::app()->debug($message, $category);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-20
|
||||
* Time: 10:25
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
/**
|
||||
* Interface ErrorInterface
|
||||
* @package Kiri\Kiri\Error
|
||||
*/
|
||||
interface ErrorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param int $code
|
||||
* @return mixed
|
||||
*/
|
||||
public function sendError($message, $file, $line, $code = 500): mixed;
|
||||
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-20
|
||||
* Time: 10:25
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
/**
|
||||
* Interface ErrorInterface
|
||||
* @package Kiri\Kiri\Error
|
||||
*/
|
||||
interface ErrorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param int $code
|
||||
* @return mixed
|
||||
*/
|
||||
public function sendError($message, $file, $line, $code = 500): mixed;
|
||||
|
||||
}
|
||||
|
||||
+117
-117
@@ -1,117 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-22
|
||||
* Time: 14:36
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class Logger
|
||||
* @package Kiri\Kiri\Error
|
||||
* @mixin \Kiri\Abstracts\Logger
|
||||
*/
|
||||
class Logger extends Component
|
||||
{
|
||||
|
||||
private array $logs = [];
|
||||
|
||||
|
||||
/**
|
||||
* inject logger
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
#[Inject(LoggerInterface::class)]
|
||||
public LoggerInterface $logger;
|
||||
|
||||
|
||||
private array $sources = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $application
|
||||
* @return string
|
||||
*/
|
||||
public function getLastError(string $application = 'app'): string
|
||||
{
|
||||
return $this->logs[$application] ?? 'Unknown error.';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $method
|
||||
* @return void
|
||||
*/
|
||||
public function fail($message, $method)
|
||||
{
|
||||
$this->logs[$method] = $message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $messages
|
||||
* @param string $method
|
||||
* @throws Exception
|
||||
*/
|
||||
public function write(string $messages, string $method = 'app')
|
||||
{
|
||||
if (empty($messages)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$to_day = date('Y-m-d');
|
||||
|
||||
$fileName = storage('server-' . $to_day . '.log', $dirName = 'log/' . ($method ?? 'app'));
|
||||
|
||||
file_put_contents($fileName, '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Throwable $exception
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exception(Throwable $exception): mixed
|
||||
{
|
||||
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
||||
|
||||
$logger = Kiri::app()->getLogger();
|
||||
$logger->write(jTraceEx($exception), 'exception');
|
||||
|
||||
return Json::to($code, $exception->getMessage(), [
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine()
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $name, array $arguments): mixed
|
||||
{
|
||||
if (!method_exists($this, $name)) {
|
||||
return $this->logger->{$name}(...$arguments);
|
||||
} else {
|
||||
return $this->{$name}(...$arguments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-22
|
||||
* Time: 14:36
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class Logger
|
||||
* @package Kiri\Kiri\Error
|
||||
* @mixin \Kiri\Abstracts\Logger
|
||||
*/
|
||||
class Logger extends Component
|
||||
{
|
||||
|
||||
private array $logs = [];
|
||||
|
||||
|
||||
/**
|
||||
* inject logger
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
#[Inject(LoggerInterface::class)]
|
||||
public LoggerInterface $logger;
|
||||
|
||||
|
||||
private array $sources = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $application
|
||||
* @return string
|
||||
*/
|
||||
public function getLastError(string $application = 'app'): string
|
||||
{
|
||||
return $this->logs[$application] ?? 'Unknown error.';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $method
|
||||
* @return void
|
||||
*/
|
||||
public function fail($message, $method)
|
||||
{
|
||||
$this->logs[$method] = $message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $messages
|
||||
* @param string $method
|
||||
* @throws Exception
|
||||
*/
|
||||
public function write(string $messages, string $method = 'app')
|
||||
{
|
||||
if (empty($messages)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$to_day = date('Y-m-d');
|
||||
|
||||
$fileName = storage('server-' . $to_day . '.log', $dirName = 'log/' . ($method ?? 'app'));
|
||||
|
||||
file_put_contents($fileName, '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Throwable $exception
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exception(Throwable $exception): mixed
|
||||
{
|
||||
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
||||
|
||||
$logger = Kiri::app()->getLogger();
|
||||
$logger->write(jTraceEx($exception), 'exception');
|
||||
|
||||
return Json::to($code, $exception->getMessage(), [
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine()
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $name, array $arguments): mixed
|
||||
{
|
||||
if (!method_exists($this, $name)) {
|
||||
return $this->logger->{$name}(...$arguments);
|
||||
} else {
|
||||
return $this->{$name}(...$arguments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,53 +1,53 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Http\Aspect\OnAspectInterface;
|
||||
use Http\Aspect\OnJoinPointInterface;
|
||||
use Http\Constrict\RequestInterface;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Class LoggerAspect
|
||||
* @package Kiri\Error
|
||||
*/
|
||||
class LoggerAspect implements OnAspectInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param OnJoinPointInterface $joinPoint
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process(OnJoinPointInterface $joinPoint): mixed
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$response = $joinPoint->process();
|
||||
|
||||
$this->print_runtime($time);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $startTime
|
||||
* @throws Exception
|
||||
*/
|
||||
private function print_runtime($startTime)
|
||||
{
|
||||
$request = Kiri::getDi()->get(RequestInterface::class);
|
||||
$runTime = round(microtime(true) - $startTime, 6);
|
||||
|
||||
$logger = Kiri::getDi()->get(LoggerInterface::class);
|
||||
$logger->debug(sprintf('run %s use time %6f', $request->getUri()->__toString(), $runTime));
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Http\Aspect\OnAspectInterface;
|
||||
use Http\Aspect\OnJoinPointInterface;
|
||||
use Http\Constrict\RequestInterface;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Class LoggerAspect
|
||||
* @package Kiri\Error
|
||||
*/
|
||||
class LoggerAspect implements OnAspectInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param OnJoinPointInterface $joinPoint
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process(OnJoinPointInterface $joinPoint): mixed
|
||||
{
|
||||
$time = microtime(true);
|
||||
|
||||
$response = $joinPoint->process();
|
||||
|
||||
$this->print_runtime($time);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $startTime
|
||||
* @throws Exception
|
||||
*/
|
||||
private function print_runtime($startTime)
|
||||
{
|
||||
$request = Kiri::getDi()->get(RequestInterface::class);
|
||||
$runTime = round(microtime(true) - $startTime, 6);
|
||||
|
||||
$logger = Kiri::getDi()->get(LoggerInterface::class);
|
||||
$logger->debug(sprintf('run %s use time %6f', $request->getUri()->__toString(), $runTime));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,84 +1,84 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Exception\ComponentException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\BaseProcess;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
|
||||
/**
|
||||
* Class LoggerProcess
|
||||
* @package Kiri\Error
|
||||
*/
|
||||
class LoggerProcess extends BaseProcess
|
||||
{
|
||||
|
||||
|
||||
public string $name = 'logger process';
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function process(Process $process): void
|
||||
{
|
||||
// TODO: Implement onHandler() method.
|
||||
$this->message($process);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function message(Process $process)
|
||||
{
|
||||
if ($this->isStop()) {
|
||||
return;
|
||||
}
|
||||
$message = Json::decode($process->read());
|
||||
if (!empty($message)) {
|
||||
Kiri::writeFile($this->getDirName($message), $message[0], FILE_APPEND);
|
||||
|
||||
$this->checkLogFile($message[1]);
|
||||
}
|
||||
|
||||
Coroutine\System::sleep(1);
|
||||
|
||||
$this->message($process);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getDirName($message): string
|
||||
{
|
||||
return storage('server-' . date('Y-m-d') . '.log', $message[1]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $dirName
|
||||
* @throws Exception
|
||||
*/
|
||||
private function checkLogFile($dirName)
|
||||
{
|
||||
$files = new \DirectoryIterator(storage(null, $dirName));
|
||||
if ($files->getSize() < 15) {
|
||||
return;
|
||||
}
|
||||
Coroutine\System::exec('find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;');
|
||||
}
|
||||
|
||||
}
|
||||
<?php
|
||||
|
||||
|
||||
namespace Kiri\Error;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Exception\ComponentException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\BaseProcess;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
|
||||
/**
|
||||
* Class LoggerProcess
|
||||
* @package Kiri\Error
|
||||
*/
|
||||
class LoggerProcess extends BaseProcess
|
||||
{
|
||||
|
||||
|
||||
public string $name = 'logger process';
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function process(Process $process): void
|
||||
{
|
||||
// TODO: Implement onHandler() method.
|
||||
$this->message($process);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Process $process
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function message(Process $process)
|
||||
{
|
||||
if ($this->isStop()) {
|
||||
return;
|
||||
}
|
||||
$message = Json::decode($process->read());
|
||||
if (!empty($message)) {
|
||||
Kiri::writeFile($this->getDirName($message), $message[0], FILE_APPEND);
|
||||
|
||||
$this->checkLogFile($message[1]);
|
||||
}
|
||||
|
||||
Coroutine\System::sleep(1);
|
||||
|
||||
$this->message($process);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getDirName($message): string
|
||||
{
|
||||
return storage('server-' . date('Y-m-d') . '.log', $message[1]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $dirName
|
||||
* @throws Exception
|
||||
*/
|
||||
private function checkLogFile($dirName)
|
||||
{
|
||||
$files = new \DirectoryIterator(storage(null, $dirName));
|
||||
if ($files->getSize() < 15) {
|
||||
return;
|
||||
}
|
||||
Coroutine\System::exec('find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user