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

297 lines
5.9 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;
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
{
2020-10-29 18:17:25 +08:00
private array $logs = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
public int $worker_id;
2020-08-31 01:27:08 +08:00
/**
* @param $message
2020-12-17 14:09:14 +08:00
* @param string $method
* @param null $file
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function debug(mixed $message, string $method = 'app', $file = null)
2020-08-31 01:27:08 +08:00
{
2020-12-17 14:09:14 +08:00
$this->writer($message, $method);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
2020-12-17 14:09:14 +08:00
* @param string $method
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function trance($message, $method = 'app')
2020-08-31 01:27:08 +08:00
{
2020-12-17 14:09:14 +08:00
$this->writer($message, $method);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
2020-12-17 14:09:14 +08:00
* @param string $method
* @param null $file
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function error(mixed $message, $method = 'error', $file = null)
2020-08-31 01:27:08 +08:00
{
2020-12-17 14:09:14 +08:00
$this->writer($message, $method);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
2020-12-17 14:09:14 +08:00
* @param string $method
* @param null $file
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function success(mixed $message, $method = 'app', $file = null)
2020-08-31 01:27:08 +08:00
{
2020-12-17 14:09:14 +08:00
$this->writer($message, $method);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
2020-12-17 14:09:14 +08:00
* @param string $method
2020-08-31 01:27:08 +08:00
* @return string
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function writer($message, $method = 'app'): string
2020-08-31 01:27:08 +08:00
{
2020-12-17 14:09:14 +08:00
$this->print_r($message, $method);
2020-10-30 01:25:38 +08:00
if ($message instanceof Throwable) {
2020-08-31 01:27:08 +08:00
$message = $message->getMessage();
} else {
if (is_array($message) || is_object($message)) {
2020-09-02 11:38:47 +08:00
$message = $this->arrayFormat($message);
2020-08-31 01:27:08 +08:00
}
}
if (is_array($message)) {
2020-09-02 11:38:47 +08:00
$message = $this->arrayFormat($message);
2020-08-31 01:27:08 +08:00
}
if (!empty($message)) {
2020-09-02 11:42:40 +08:00
if (!is_array($this->logs)) {
$this->logs = [];
2020-08-31 01:27:08 +08:00
}
2020-12-17 14:09:14 +08:00
$this->logs[] = [$method, $message];
2020-08-31 01:27:08 +08:00
}
return $message;
}
/**
* @param $message
2020-12-17 14:09:14 +08:00
* @param $method
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function print_r($message, $method = '')
2020-08-31 01:27:08 +08:00
{
2020-09-07 16:59:58 +08:00
$debug = Config::get('debug', false, ['enable' => false]);
if ((bool)$debug['enable'] === true) {
2020-09-07 16:55:46 +08:00
if (!is_callable($debug['callback'] ?? null, true)) {
2020-09-07 16:55:31 +08:00
return;
}
2020-12-17 14:09:14 +08:00
call_user_func($debug['callback'], $message, $method);
2020-09-07 16:55:31 +08:00
}
2020-08-31 01:27:08 +08:00
}
2021-02-23 11:12:11 +08:00
/**
* @param $message
*/
public function output($message)
{
2021-03-23 18:38:40 +08:00
if (str_contains($message, 'Event::rshutdown(): Event::wait()')) {
return;
}
2021-02-23 11:25:02 +08:00
echo $message;
2021-02-23 11:12:11 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param string $application
* @return mixed
*/
2020-12-17 14:09:14 +08:00
public function getLastError($application = 'app'): mixed
2020-08-31 01:27:08 +08:00
{
2020-12-17 14:09:14 +08:00
$filetype = [];
2020-09-02 11:38:47 +08:00
foreach ($this->logs as $key => $val) {
2020-08-31 01:27:08 +08:00
if ($val[0] != $application) {
continue;
}
2020-12-17 14:09:14 +08:00
$filetype[] = $val[1];
2020-08-31 01:27:08 +08:00
}
2020-12-17 14:09:14 +08:00
if (empty($filetype)) {
2020-08-31 01:27:08 +08:00
return 'Unknown error.';
}
2020-12-17 14:09:14 +08:00
return end($filetype);
2020-08-31 01:27:08 +08:00
}
/**
* @param $messages
2020-12-17 14:09:14 +08:00
* @param string $method
2020-09-10 19:26:42 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function write(string $messages, $method = 'app')
2020-08-31 01:27:08 +08:00
{
if (empty($messages)) {
return;
}
2021-03-02 19:16:20 +08:00
2021-03-05 14:58:49 +08:00
$fileName = 'server-' . date('Y-m-d') . '.log';
$dirName = 'log/' . (empty($method) ? 'app' : $method);
$logFile = '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL;
Snowflake::writeFile(storage($fileName, $dirName), $logFile, FILE_APPEND);
$files = glob(storage(null, $dirName) . '/*');
if (count($files) >= 15) {
$command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;';
if (Context::inCoroutine()) {
Coroutine\System::exec($command);
} else {
\shell_exec($command);
}
2021-03-02 19:19:21 +08:00
}
2020-08-31 01:27:08 +08:00
}
/**
* @param $logFile
2020-12-17 14:09:14 +08:00
* @return string
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
private function getSource($logFile): string
2020-08-31 01:27:08 +08:00
{
if (!file_exists($logFile)) {
2021-03-02 13:40:27 +08:00
Coroutine\System::exec('echo 3 > /proc/sys/vm/drop_caches');
2020-08-31 01:27:08 +08:00
touch($logFile);
}
if (is_writeable($logFile)) {
$logFile = realpath($logFile);
}
return $logFile;
}
2021-03-02 13:42:01 +08:00
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
* 写入日志
*/
2020-09-02 11:38:47 +08:00
public function insert()
2020-08-31 01:27:08 +08:00
{
2020-09-02 11:38:47 +08:00
if (empty($this->logs)) {
2020-08-31 01:27:08 +08:00
return;
}
2020-09-02 11:38:47 +08:00
foreach ($this->logs as $log) {
2020-12-17 14:09:14 +08:00
[$method, $message] = $log;
$this->write($message, $method);
2020-08-31 01:27:08 +08:00
}
2020-09-02 11:38:47 +08:00
$this->logs = [];
2020-08-31 01:27:08 +08:00
}
/**
* @return array
*/
2020-12-17 14:09:14 +08:00
public function clear(): array
2020-08-31 01:27:08 +08:00
{
2020-09-02 11:38:47 +08:00
return $this->logs = [];
2020-08-31 01:27:08 +08:00
}
/**
* @param $data
* @return string
*/
2020-12-17 14:09:14 +08:00
private function arrayFormat($data): string
2020-08-31 01:27:08 +08:00
{
if (is_string($data)) {
return $data;
}
2020-11-06 16:47:17 +08:00
if ($data instanceof Throwable) {
2020-09-02 11:38:47 +08:00
$data = $this->getException($data);
2020-08-31 01:27:08 +08:00
} else if (is_object($data)) {
$data = get_object_vars($data);
}
2020-12-17 14:09:14 +08:00
$filetype = [];
2020-08-31 01:27:08 +08:00
foreach ($data as $key => $val) {
if (is_array($val)) {
2020-12-17 14:09:14 +08:00
$filetype[] = $this->arrayFormat($val);
2020-08-31 01:27:08 +08:00
} else {
2020-12-17 14:09:14 +08:00
$filetype[] = (is_string($key) ? $key . ' : ' : '') . $val;
2020-08-31 01:27:08 +08:00
}
}
2020-12-17 14:09:14 +08:00
return implode(PHP_EOL, $filetype);
2020-08-31 01:27:08 +08:00
}
2020-09-09 12:08:07 +08:00
/**
2020-10-30 01:25:38 +08:00
* @param Throwable $exception
2020-12-16 16:32:05 +08:00
* @return mixed
2020-09-09 12:08:07 +08:00
* @throws Exception
*/
2020-12-16 16:32:05 +08:00
public function exception(Throwable $exception): mixed
2020-09-09 12:08:07 +08:00
{
$errorInfo = [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine()
];
$this->error(var_export($errorInfo, true));
2020-12-29 17:30:32 +08:00
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
2020-09-09 12:08:07 +08:00
2021-02-20 13:08:54 +08:00
$logger = Snowflake::app()->getLogger();
2020-09-09 12:08:07 +08:00
$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');
2020-12-24 11:12:23 +08:00
return Json::to($code, $errorInfo['message'], [
2020-12-16 16:24:57 +08:00
'file' => $exception->getFile(),
'line' => $exception->getLine()
]);
2020-09-09 12:08:07 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2020-11-06 16:47:17 +08:00
* @param Throwable $exception
2020-08-31 01:27:08 +08:00
* @return array
*/
2020-12-17 14:09:14 +08:00
private function getException(Throwable $exception): array
2020-08-31 01:27:08 +08:00
{
2020-12-17 14:09:14 +08:00
$filetype = [$exception->getMessage()];
$filetype[] = $exception->getFile() . ' on line ' . $exception->getLine();
$filetype[] = $exception->getTrace();
return $filetype;
2020-08-31 01:27:08 +08:00
}
}