Files
kiri-core/System/Error/LoggerProcess.php
T

90 lines
1.6 KiB
PHP
Raw Normal View History

2021-03-02 19:16:20 +08:00
<?php
2021-08-11 01:04:57 +08:00
namespace Kiri\Error;
2021-03-02 19:16:20 +08:00
2021-03-02 19:22:29 +08:00
use Exception;
2021-04-26 17:37:53 +08:00
use JetBrains\PhpStorm\Pure;
2021-08-11 01:04:57 +08:00
use Kiri\Core\Json;
use Kiri\Exception\ComponentException;
use Kiri\Kiri;
2021-03-02 19:16:20 +08:00
use Swoole\Coroutine;
2021-08-13 14:58:58 +08:00
use Swoole\Process;
2021-08-19 15:42:47 +08:00
use Server\Abstracts\CustomProcess;
2021-03-02 19:16:20 +08:00
/**
* Class LoggerProcess
2021-08-11 01:04:57 +08:00
* @package Kiri\Error
2021-03-02 19:16:20 +08:00
*/
2021-08-19 15:42:47 +08:00
class LoggerProcess extends CustomProcess
2021-03-02 19:16:20 +08:00
{
2021-04-26 17:37:53 +08:00
/**
2021-08-19 15:42:47 +08:00
* @param Process $process
2021-04-26 17:37:53 +08:00
* @return string
*/
2021-08-13 14:58:58 +08:00
#[Pure] public function getProcessName(Process $process): string
2021-04-26 17:34:33 +08:00
{
// TODO: Implement getProcessName() method.
2021-04-26 17:37:53 +08:00
return get_called_class();
2021-04-26 17:34:33 +08:00
}
2021-03-02 19:16:20 +08:00
/**
2021-08-13 14:58:58 +08:00
* @param Process $process
2021-03-02 19:16:20 +08:00
* @throws ComponentException
*/
2021-08-13 14:58:58 +08:00
public function onHandler(Process $process): void
2021-03-02 19:16:20 +08:00
{
// TODO: Implement onHandler() method.
$this->message($process);
}
/**
2021-08-13 14:58:58 +08:00
* @param Process $process
2021-03-02 19:16:20 +08:00
* @throws ComponentException
2021-03-02 19:22:29 +08:00
* @throws Exception
2021-03-02 19:16:20 +08:00
*/
2021-08-13 14:58:58 +08:00
public function message(Process $process)
2021-03-02 19:16:20 +08:00
{
$message = Json::decode($process->read());
if (!empty($message)) {
2021-08-11 01:04:57 +08:00
Kiri::writeFile($this->getDirName($message), $message[0], FILE_APPEND);
2021-03-02 19:22:29 +08:00
$this->checkLogFile($message[1]);
2021-03-02 19:16:20 +08:00
}
Coroutine\System::sleep(1);
$this->message($process);
}
2021-03-02 19:22:29 +08:00
/**
* @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)
{
2021-03-02 19:25:53 +08:00
$files = new \DirectoryIterator(storage(null, $dirName));
2021-03-02 19:22:29 +08:00
if ($files->getSize() < 15) {
return;
}
Coroutine\System::exec('find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;');
}
2021-03-02 19:16:20 +08:00
}