改名
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/4/26 0026
|
||||
* Time: 10:00
|
||||
*/
|
||||
|
||||
namespace Snowflake\Error;
|
||||
|
||||
use Exception;
|
||||
use HttpServer\IInterface\IFormatter;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class ErrorHandler
|
||||
*
|
||||
* @package Snowflake\Snowflake\Base
|
||||
* @property-read $asError
|
||||
*/
|
||||
class ErrorHandler extends Component implements ErrorInterface
|
||||
{
|
||||
|
||||
/** @var IFormatter $message */
|
||||
private $message = NULL;
|
||||
|
||||
public $action;
|
||||
|
||||
public $category = 'app';
|
||||
|
||||
/**
|
||||
* 错误处理注册
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
ini_set('display_errors', 0);
|
||||
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 ($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 Exception $exception
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function exceptionHandler($exception)
|
||||
{
|
||||
$this->category = 'exception';
|
||||
|
||||
$event = Snowflake::app()->event;
|
||||
$event->trigger(Event::RELEASE_ALL);
|
||||
|
||||
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*
|
||||
* 以异常形式抛出错误,防止执行后续程序
|
||||
*/
|
||||
public function errorHandler()
|
||||
{
|
||||
$error = func_get_args();
|
||||
if (strpos($error[2], 'vendor/Reboot.php') !== FALSE) {
|
||||
return;
|
||||
}
|
||||
|
||||
$path = ['file' => $error[2], 'line' => $error[3]];
|
||||
|
||||
if ($error[0] === 0) {
|
||||
$error[0] = 500;
|
||||
}
|
||||
|
||||
$data = JSON::to(500, 'Error : ' . $error[1], $path);
|
||||
|
||||
Snowflake::app()->getLogger()->error($data, 'error');
|
||||
|
||||
$event = Snowflake::app()->event;
|
||||
$event->trigger(Event::RELEASE_ALL);
|
||||
|
||||
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)
|
||||
{
|
||||
$path = ['file' => $file, 'line' => $line];
|
||||
|
||||
$data = JSON::to($code, $this->category . ': ' . $message, $path);
|
||||
|
||||
Snowflake::app()->getLogger()->trance($data, $this->category);
|
||||
|
||||
return response()->send($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getErrorMessage()
|
||||
{
|
||||
$message = $this->message;
|
||||
$this->message = NULL;
|
||||
return $message->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getAsError()
|
||||
{
|
||||
return $this->message !== NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $category
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function writer($message, $category = 'app')
|
||||
{
|
||||
Snowflake::app()->getLogger()->debug($message, $category);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-20
|
||||
* Time: 10:25
|
||||
*/
|
||||
|
||||
namespace Snowflake\Error;
|
||||
|
||||
/**
|
||||
* Interface ErrorInterface
|
||||
* @package Snowflake\Snowflake\Error
|
||||
*/
|
||||
interface ErrorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param int $code
|
||||
* @return mixed
|
||||
*/
|
||||
public function sendError($message, $file, $line, $code = 500);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: admin
|
||||
* Date: 2019-03-22
|
||||
* Time: 14:36
|
||||
*/
|
||||
|
||||
namespace Snowflake\Error;
|
||||
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Core\JSON;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Process;
|
||||
|
||||
/**
|
||||
* Class Logger
|
||||
* @package Snowflake\Snowflake\Error
|
||||
*/
|
||||
class Logger extends Component
|
||||
{
|
||||
|
||||
private $logs = [];
|
||||
|
||||
public $worker_id;
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
* @param null $_
|
||||
* @throws Exception
|
||||
*/
|
||||
public function debug($message, $category = 'app', $_ = null)
|
||||
{
|
||||
parent::debug($message);
|
||||
$this->writer($message, $category);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trance($message, $category = 'app')
|
||||
{
|
||||
$this->writer($message, $category);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
* @param null $_
|
||||
* @throws Exception
|
||||
*/
|
||||
public function error($message, $category = 'error', $_ = null)
|
||||
{
|
||||
parent::error($message);
|
||||
$this->writer($message, $category);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
* @param null $_
|
||||
* @throws Exception
|
||||
*/
|
||||
public function success($message, $category = 'app', $_ = null)
|
||||
{
|
||||
parent::success($message);
|
||||
$this->writer($message, $category);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param string $category
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function writer($message, $category = 'app')
|
||||
{
|
||||
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[] = [$category, $message];
|
||||
}
|
||||
return $message;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param $category
|
||||
* @throws Exception
|
||||
*/
|
||||
public function print_r($message, $category = '')
|
||||
{
|
||||
/** @var Process $logger */
|
||||
$logger = Snowflake::app()->logger;
|
||||
$logger->write(JSON::encode([$message, $category]));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $application
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLastError($application = 'app')
|
||||
{
|
||||
$_tmp = [];
|
||||
foreach ($this->logs as $key => $val) {
|
||||
if ($val[0] != $application) {
|
||||
continue;
|
||||
}
|
||||
$_tmp[] = $val[1];
|
||||
}
|
||||
if (empty($_tmp)) {
|
||||
return 'Unknown error.';
|
||||
}
|
||||
return end($_tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $messages
|
||||
* @param string $category
|
||||
* @throws Exception
|
||||
*/
|
||||
public function write(string $messages, $category = 'app')
|
||||
{
|
||||
if (empty($messages)) {
|
||||
return;
|
||||
}
|
||||
$fileName = 'server-' . date('Y-m-d') . '.log';
|
||||
$dirName = 'log/' . (empty($category) ? 'app' : $category);
|
||||
$logFile = '[' . date('Y-m-d H:i:s') . ']:' . PHP_EOL . $messages . PHP_EOL;
|
||||
Snowflake::writeFile(storage($fileName, $dirName), $logFile, FILE_APPEND);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $logFile
|
||||
* @return false|string
|
||||
*/
|
||||
private function getSource($logFile)
|
||||
{
|
||||
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
|
||||
* 写入日志
|
||||
*/
|
||||
public function insert()
|
||||
{
|
||||
if (empty($this->logs)) {
|
||||
return;
|
||||
}
|
||||
foreach ($this->logs as $log) {
|
||||
[$category, $message] = $log;
|
||||
$this->write($message, $category);
|
||||
}
|
||||
$this->logs = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
return $this->logs = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
private function arrayFormat($data)
|
||||
{
|
||||
if (is_string($data)) {
|
||||
return $data;
|
||||
}
|
||||
if ($data instanceof Exception) {
|
||||
$data = $this->getException($data);
|
||||
} else if (is_object($data)) {
|
||||
$data = get_object_vars($data);
|
||||
}
|
||||
|
||||
$_tmp = [];
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_array($val)) {
|
||||
$_tmp[] = $this->arrayFormat($val);
|
||||
} else {
|
||||
$_tmp[] = (is_string($key) ? $key . ' : ' : '') . $val;
|
||||
}
|
||||
}
|
||||
return implode(PHP_EOL, $_tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Exception $exception
|
||||
* @return array
|
||||
*/
|
||||
private function getException($exception)
|
||||
{
|
||||
$_tmp = [$exception->getMessage()];
|
||||
$_tmp[] = $exception->getFile() . ' on line ' . $exception->getLine();
|
||||
$_tmp[] = $exception->getTrace();
|
||||
return $_tmp;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user