This commit is contained in:
2021-11-18 15:37:11 +08:00
parent 1643a839b3
commit e3348a11c8
2 changed files with 195 additions and 181 deletions
+191 -167
View File
@@ -21,199 +21,223 @@ use Swoole\Coroutine;
class BaseObject implements Configure class BaseObject implements Configure
{ {
/** /**
* BaseAbstract constructor. * BaseAbstract constructor.
* *
* @param array $config * @param array $config
* @throws Exception * @throws Exception
*/ */
public function __construct(array $config = []) public function __construct(array $config = [])
{ {
if (!empty($config) && is_array($config)) { if (!empty($config) && is_array($config)) {
Kiri::configure($this, $config); Kiri::configure($this, $config);
} }
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function init() public function init()
{ {
} }
/** /**
* @param array|callable $callback * @param array|callable $callback
* @param object $scope * @param object $scope
*/ */
public function async_create(array|callable $callback, object $scope) public function async_create(array|callable $callback, object $scope)
{ {
Coroutine::create($callback, $scope); Coroutine::create($callback, $scope);
} }
/** /**
* @return string * @return string
*/ */
#[Pure] public static function className(): string #[Pure] public static function className(): string
{ {
return static::class; return static::class;
} }
/** /**
* @param $name * @param $name
* @param $value * @param $value
* *
* @throws Exception * @throws Exception
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
$method = 'set' . ucfirst($name); $method = 'set' . ucfirst($name);
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$this->{$method}($value); $this->{$method}($value);
} else { } else {
throw new Exception('The set name ' . $name . ' not find in class ' . static::class); throw new Exception('The set name ' . $name . ' not find in class ' . static::class);
} }
} }
/** /**
* @param $name * @param $name
* *
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
$method = 'get' . ucfirst($name); $method = 'get' . ucfirst($name);
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
return $this->$method(); return $this->$method();
} else { } else {
throw new Exception('The get name ' . $name . ' not find in class ' . static::class); throw new Exception('The get name ' . $name . ' not find in class ' . static::class);
} }
} }
/** /**
* @param $message * @param $message
* @param string $model * @param string $model
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function addError($message, string $model = 'app'): bool public function addError($message, string $model = 'app'): bool
{ {
if ($message instanceof \Throwable) { if ($message instanceof \Throwable) {
$this->error(jTraceEx($message)); $this->error(jTraceEx($message));
} else { } else {
if (!is_string($message)) { if (!is_string($message)) {
$message = json_encode($message, JSON_UNESCAPED_UNICODE); $message = json_encode($message, JSON_UNESCAPED_UNICODE);
} }
$this->error($message); $this->error($message);
} }
return FALSE; return FALSE;
} }
/** /**
* @return Logger * @return Logger
* @throws Exception * @throws Exception
*/ */
private function logger(): Logger private function logger(): Logger
{ {
return Kiri::getDi()->get(Logger::class); return Kiri::getDi()->get(Logger::class);
} }
/** /**
* @param mixed $message * @param mixed $message
* @param string $method * @param string $method
* @param string $file * @param string $file
* @throws Exception * @throws Exception
*/ */
public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__) public function debug(mixed $message, string $method = '', string $file = '')
{ {
if (!is_string($message)) { if (!is_string($message)) {
$message = print_r($message, true); $message = print_r($message, true);
} }
$message = "\033[35m[" . date('Y-m-d H:i:s') . '][DEBUG]: ' . $message . "\033[0m"; $message = "\033[35m[" . date('Y-m-d H:i:s') . '][DEBUG]: ' . $message . "\033[0m";
$this->logger()->debug(Logger::DEBUG, [$message, $method, $file]);
}
/** $context = [];
* @param mixed $message if (!empty($method)) $context['method'] = $method;
* @param string $method if (!empty($file)) $context['file'] = $file;
* @param string $file
* @throws Exception
*/
public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{
if (!is_string($message)) {
$message = print_r($message, true);
}
$message = "\033[34m[" . date('Y-m-d H:i:s') . '][INFO]: ' . $message . "\033[0m";
$this->logger()->info(Logger::NOTICE, [$message, $method, $file]); $this->logger()->debug($message, $context);
} }
/** /**
* @param mixed $message * @param mixed $message
* @param string $method * @param string $method
* @param string $file * @param string $file
* @throws Exception * @throws Exception
*/ */
public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__) public function info(mixed $message, string $method = '', string $file = '')
{ {
if (!is_string($message)) { if (!is_string($message)) {
$message = print_r($message, true); $message = print_r($message, true);
} }
$message = "\033[34m[" . date('Y-m-d H:i:s') . '][INFO]: ' . $message . "\033[0m";
$message = "\033[36m[" . date('Y-m-d H:i:s') . '][SUCCESS]: ' . $message . "\033[0m";
$this->logger()->notice(Logger::NOTICE, [$message, $method, $file]);
}
/** $context = [];
* @param mixed $message if (!empty($method)) $context['method'] = $method;
* @param string $method if (!empty($file)) $context['file'] = $file;
* @param string $file
* @throws Exception
*/
public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{
if (!is_string($message)) {
$message = print_r($message, true);
}
$message = "\033[33m[" . date('Y-m-d H:i:s') . '][WARNING]: ' . $message . "\033[0m"; $this->logger()->info($message, $context);
}
$this->logger()->critical(Logger::NOTICE, [$message, $method, $file]);
}
/** /**
* @param mixed $message * @param mixed $message
* @param null $method * @param string $method
* @param null $file * @param string $file
* @throws Exception * @throws Exception
*/ */
public function error(mixed $message, $method = null, $file = null) public function success(mixed $message, string $method = '', string $file = '')
{ {
if ($message instanceof \Throwable) { if (!is_string($message)) {
$message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile(); $message = print_r($message, true);
} }
$content = (empty($method) ? '' : $method . ': ') . $message;
$message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m"; $message = "\033[36m[" . date('Y-m-d H:i:s') . '][SUCCESS]: ' . $message . "\033[0m";
if (!empty($file)) {
$message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m";
}
$this->logger()->error(Logger::ERROR, [$message, $method, $file]); $context = [];
} if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->notice($message, $context);
}
/**
* @param mixed $message
* @param string $method
* @param string $file
* @throws Exception
*/
public function warning(mixed $message, string $method = '', string $file = '')
{
if (!is_string($message)) {
$message = print_r($message, true);
}
$message = "\033[33m[" . date('Y-m-d H:i:s') . '][WARNING]: ' . $message . "\033[0m";
$context = [];
if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->critical($message, $context);
}
/**
* @param mixed $message
* @param null $method
* @param null $file
* @throws Exception
*/
public function error(mixed $message, $method = null, $file = null)
{
if ($message instanceof \Throwable) {
$message = $message->getMessage() . " on line " . $message->getLine() . " at file " . $message->getFile();
}
$content = (empty($method) ? '' : $method . ': ') . $message;
$message = "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $content . "\033[0m";
if (!empty($file)) {
$message .= PHP_EOL . "\033[41;37m[" . date('Y-m-d H:i:s') . '][ERROR]: ' . $file . "\033[0m";
}
$context = [];
if (!empty($method)) $context['method'] = $method;
if (!empty($file)) $context['file'] = $file;
$this->logger()->error($message, $context);
}
} }
+4 -14
View File
@@ -20,22 +20,16 @@ use Server\Abstracts\BaseProcess;
class LoggerProcess extends BaseProcess class LoggerProcess extends BaseProcess
{ {
/**
* @param Process $process public string $name = 'logger process';
* @return string
*/
#[Pure] public function getProcessName(Process $process): string
{
// TODO: Implement getProcessName() method.
return get_called_class();
}
/** /**
* @param Process $process * @param Process $process
* @throws ComponentException * @throws ComponentException
*/ */
public function onHandler(Process $process): void public function onProcessExec(Process $process): void
{ {
// TODO: Implement onHandler() method. // TODO: Implement onHandler() method.
$this->message($process); $this->message($process);
@@ -49,10 +43,6 @@ class LoggerProcess extends BaseProcess
*/ */
public function message(Process $process) public function message(Process $process)
{ {
if ($this->checkProcessIsStop()) {
$this->exit();
return;
}
$message = Json::decode($process->read()); $message = Json::decode($process->read());
if (!empty($message)) { if (!empty($message)) {
Kiri::writeFile($this->getDirName($message), $message[0], FILE_APPEND); Kiri::writeFile($this->getDirName($message), $message[0], FILE_APPEND);