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

279 lines
5.5 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;
2020-11-18 15:09:25 +08:00
use Snowflake\Abstracts\BaseObject;
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-08-31 12:38:32 +08:00
use Snowflake\Core\JSON;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
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:09:25 +08:00
class Logger extends BaseObject
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
* @param string $category
2020-09-02 11:38:47 +08:00
* @param null $_
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-09-02 11:38:47 +08:00
public function debug($message, $category = 'app', $_ = null)
2020-08-31 01:27:08 +08:00
{
2020-09-02 11:38:47 +08:00
$this->writer($message, $category);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
* @param string $category
* @throws Exception
*/
2020-09-02 11:38:47 +08:00
public function trance($message, $category = 'app')
2020-08-31 01:27:08 +08:00
{
2020-09-02 11:38:47 +08:00
$this->writer($message, $category);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
* @param string $category
2020-09-02 11:38:47 +08:00
* @param null $_
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-09-02 11:38:47 +08:00
public function error($message, $category = 'error', $_ = null)
2020-08-31 01:27:08 +08:00
{
2020-09-02 11:38:47 +08:00
$this->writer($message, $category);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
* @param string $category
2020-09-02 11:38:47 +08:00
* @param null $_
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-09-02 11:38:47 +08:00
public function success($message, $category = 'app', $_ = null)
2020-08-31 01:27:08 +08:00
{
2020-09-02 11:38:47 +08:00
$this->writer($message, $category);
2020-08-31 01:27:08 +08:00
}
/**
* @param $message
* @param string $category
* @return string
* @throws Exception
*/
2020-09-02 11:38:47 +08:00
private function writer($message, $category = 'app')
2020-08-31 01:27:08 +08:00
{
2020-09-07 16:55:31 +08:00
$this->print_r($message, $category);
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-09-02 11:42:40 +08:00
$this->logs[] = [$category, $message];
2020-08-31 01:27:08 +08:00
}
return $message;
}
/**
* @param $message
* @param $category
* @throws Exception
*/
2020-09-02 11:38:47 +08:00
public function print_r($message, $category = '')
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;
}
call_user_func($debug['callback'], $message, $category);
}
2020-08-31 01:27:08 +08:00
}
/**
* @param string $application
* @return mixed
*/
2020-09-02 11:38:47 +08:00
public function getLastError($application = 'app')
2020-08-31 01:27:08 +08:00
{
$_tmp = [];
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;
}
$_tmp[] = $val[1];
}
if (empty($_tmp)) {
return 'Unknown error.';
}
return end($_tmp);
}
/**
* @param $messages
* @param string $category
2020-09-10 19:26:42 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-09-02 11:38:47 +08:00
public function write(string $messages, $category = 'app')
2020-08-31 01:27:08 +08:00
{
if (empty($messages)) {
return;
}
$fileName = 'server-' . date('Y-m-d') . '.log';
$dirName = 'log/' . (empty($category) ? 'app' : $category);
2020-09-03 18:52:16 +08:00
$logFile = '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL;
2020-08-31 01:27:08 +08:00
Snowflake::writeFile(storage($fileName, $dirName), $logFile, FILE_APPEND);
2020-09-16 18:30:44 +08:00
$files = glob(storage(null, $dirName) . '/*');
if (count($files) >= 5) {
$time = strtotime(date('Y-m-d', strtotime('-10days')));
2020-10-09 18:48:41 +08:00
// foreach (array_slice($files, 0, count($files) - 5) as $file) {
// if (filectime($file) < $time) {
// continue;
// }
// @unlink($file);
// }
2020-09-16 18:30:44 +08:00
}
2020-08-31 01:27:08 +08:00
}
/**
* @param $logFile
* @return false|string
*/
2020-09-02 11:38:47 +08:00
private function getSource($logFile)
2020-08-31 01:27:08 +08:00
{
if (!file_exists($logFile)) {
shell_exec('echo 3 > /proc/sys/vm/drop_caches');
touch($logFile);
}
if (is_writeable($logFile)) {
$logFile = realpath($logFile);
}
return $logFile;
}
/**
* @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-08-31 01:27:08 +08:00
[$category, $message] = $log;
2020-09-02 11:38:47 +08:00
$this->write($message, $category);
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-09-02 11:38:47 +08:00
public function clear()
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-09-02 11:38:47 +08:00
private function arrayFormat($data)
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);
}
$_tmp = [];
foreach ($data as $key => $val) {
if (is_array($val)) {
2020-09-02 11:38:47 +08:00
$_tmp[] = $this->arrayFormat($val);
2020-08-31 01:27:08 +08:00
} else {
$_tmp[] = (is_string($key) ? $key . ' : ' : '') . $val;
}
}
return implode(PHP_EOL, $_tmp);
}
2020-09-09 12:08:07 +08:00
/**
2020-10-30 01:25:38 +08:00
* @param Throwable $exception
2020-09-09 12:08:07 +08:00
* @return false|int|mixed|string
* @throws Exception
*/
2020-10-30 01:25:38 +08:00
public function exception(Throwable $exception)
2020-09-09 12:08:07 +08:00
{
$errorInfo = [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine()
];
$this->error(var_export($errorInfo, true));
$code = $exception->getCode() ?? 500;
$logger = Snowflake::app()->logger;
$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']);
}
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-11-06 16:47:17 +08:00
private function getException(Throwable $exception)
2020-08-31 01:27:08 +08:00
{
$_tmp = [$exception->getMessage()];
$_tmp[] = $exception->getFile() . ' on line ' . $exception->getLine();
$_tmp[] = $exception->getTrace();
return $_tmp;
}
}