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

329 lines
7.8 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-22
* Time: 14:36
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Error;
use Exception;
2021-03-02 13:40:27 +08:00
use HttpServer\Http\Context;
2020-09-02 11:38:47 +08:00
use Snowflake\Abstracts\Component;
2020-09-07 16:55:31 +08:00
use Snowflake\Abstracts\Config;
2020-12-24 11:12:23 +08:00
use Snowflake\Core\Json;
2021-04-23 03:38:26 +08:00
use Snowflake\Event;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
2021-02-23 11:15:47 +08:00
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
2020-08-31 01:27:08 +08:00
use Swoole\Process;
2020-10-30 01:25:38 +08:00
use Throwable;
2020-08-31 01:27:08 +08:00
/**
* Class Logger
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Error
2020-08-31 01:27:08 +08:00
*/
2020-11-18 15:11:59 +08:00
class Logger extends Component
2020-08-31 01:27:08 +08:00
{
2021-04-23 03:38:26 +08:00
private array $logs = [];
2021-07-11 02:05:56 +08:00
private array $sources = [];
2021-04-23 03:38:26 +08:00
public function init()
{
2021-04-27 15:57:50 +08:00
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']);
2021-07-11 02:05:56 +08:00
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'closeSource']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'closeSource']);
2021-04-27 15:57:50 +08:00
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']);
2021-04-23 03:38:26 +08:00
}
/**
2021-07-09 14:24:23 +08:00
* @param mixed $message
2021-04-23 03:38:26 +08:00
* @param string $method
* @param null $file
* @throws Exception
*/
public function debug(mixed $message, string $method = 'app', $file = null)
{
$this->writer($message, $method);
}
/**
2021-07-09 14:24:23 +08:00
* @param mixed $message
2021-04-23 03:38:26 +08:00
* @param string $method
* @throws Exception
*/
2021-07-09 14:24:23 +08:00
public function trance(mixed $message, string $method = 'app')
2021-04-23 03:38:26 +08:00
{
$this->writer($message, $method);
}
/**
2021-07-09 14:24:23 +08:00
* @param mixed $message
2021-04-23 03:38:26 +08:00
* @param string $method
* @param null $file
* @throws Exception
*/
public function error(mixed $message, $method = 'error', $file = null)
{
$this->writer($message, $method);
}
2021-07-11 02:05:56 +08:00
/**
* @param mixed $message
* @param string $method
* @param null $file
* @throws Exception
*/
2021-07-09 14:24:23 +08:00
public function success(mixed $message, string $method = 'app', $file = null)
2021-04-23 03:38:26 +08:00
{
$this->writer($message, $method);
}
/**
* @param $message
* @param string $method
* @return string
* @throws Exception
*/
2021-07-09 14:24:23 +08:00
private function writer($message, string $method = 'app'): string
2021-04-23 03:38:26 +08:00
{
$this->print_r($message, $method);
if ($message instanceof Throwable) {
$message = $message->getMessage();
} else {
if (is_array($message) || is_object($message)) {
$message = $this->arrayFormat($message);
}
}
if (is_array($message)) {
$message = $this->arrayFormat($message);
}
if (!empty($message)) {
if (!is_array($this->logs)) {
$this->logs = [];
}
$this->logs[] = [$method, $message];
}
return $message;
}
/**
* @param $message
2021-07-09 14:24:23 +08:00
* @param string $method
2021-04-23 03:38:26 +08:00
* @throws Exception
*/
2021-07-09 14:24:23 +08:00
public function print_r($message, string $method = '')
2021-04-23 03:38:26 +08:00
{
$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
*/
public function output($message)
{
if (str_contains($message, 'Event::rshutdown(): Event::wait()')) {
return;
}
echo $message;
}
/**
* @param string $application
* @return mixed
*/
2021-07-09 13:55:52 +08:00
public function getLastError(string $application = 'app'): mixed
2021-04-23 03:38:26 +08:00
{
$filetype = [];
foreach ($this->logs as $key => $val) {
if ($val[0] != $application) {
continue;
}
$filetype[] = $val[1];
}
if (empty($filetype)) {
return 'Unknown error.';
}
return end($filetype);
}
2021-07-11 02:05:56 +08:00
/**
* @param string $messages
* @param string $method
* @throws Exception
*/
2021-07-09 13:55:52 +08:00
public function write(string $messages, string $method = 'app')
2021-04-23 03:38:26 +08:00
{
2021-07-11 03:59:52 +08:00
return;
2021-07-11 02:05:56 +08:00
if (empty($messages)) {
2021-04-23 03:38:26 +08:00
}
2021-07-11 02:05:56 +08:00
$dirName = 'log/' . ($method ?? 'app');
$fileName = storage('server-' . date('Y-m-d') . '.log', $dirName);
if (!isset($this->sources[$fileName])) {
2021-07-11 02:09:50 +08:00
$this->sources[$fileName] = fopen($fileName, 'rw');
2021-04-23 03:38:26 +08:00
}
2021-07-11 02:05:56 +08:00
fwrite($this->sources[$fileName], '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL);
$this->clearHistoryFile($dirName);
2021-04-23 03:38:26 +08:00
}
2021-07-11 02:05:56 +08:00
/**
* 清理文件资源
*/
public function closeSource()
{
foreach ($this->sources as $source) {
fclose($source);
}
$this->sources = [];
}
/**
* @param string $dirName
* @throws \Exception
*/
private function clearHistoryFile(string $dirName)
{
$command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;';
Coroutine::getCid() !== -1 ? Coroutine\System::exec($command) : \shell_exec($command);
}
2021-04-23 03:38:26 +08:00
/**
* @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;
}
/**
* @throws Exception
* 写入日志
*/
public function insert()
{
if (empty($this->logs)) {
return;
}
foreach ($this->logs as $log) {
[$method, $message] = $log;
$this->write($message, $method);
}
$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);
}
$filetype = [];
foreach ($data as $key => $val) {
if (is_array($val)) {
$filetype[] = $this->arrayFormat($val);
} else {
$filetype[] = (is_string($key) ? $key . ' : ' : '') . $val;
}
}
return implode(PHP_EOL, $filetype);
}
/**
* @param Throwable $exception
* @return mixed
* @throws Exception
*/
public function exception(Throwable $exception): mixed
{
$errorInfo = [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine()
];
$this->error(var_export($errorInfo, true));
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
$logger = Snowflake::app()->getLogger();
$string = 'Exception: ' . PHP_EOL;
$string .= '#. message: ' . $errorInfo['message'] . PHP_EOL;
$string .= '#. file: ' . $errorInfo['file'] . PHP_EOL;
$string .= '#. line: ' . $errorInfo['line'] . PHP_EOL;
$logger->write($string . $exception->getTraceAsString(), 'trace');
$logger->write(jTraceEx($exception), 'exception');
return Json::to($code, $errorInfo['message'], [
'file' => $exception->getFile(),
'line' => $exception->getLine()
]);
}
/**
* @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;
}
2020-08-31 01:27:08 +08:00
}