This commit is contained in:
2023-04-18 23:47:31 +08:00
parent 88c2c430c6
commit af284dbe4b
6 changed files with 62 additions and 37 deletions
+24 -1
View File
@@ -1156,13 +1156,36 @@ if (!function_exists('error')) {
* @param mixed $message
* @param array $method
* @return void
* @throws ReflectionException
*/
function error(mixed $message, array $method = []): void
{
if ($message instanceof Throwable) {
$message = throwable($message);
}
Kiri::getLogger()->error($message, $method);
}
}
if (!function_exists('addError')) {
/**
* @param mixed $message
* @param string $method
* @return bool
* @throws ReflectionException
*/
function addError(mixed $message, string $method = 'app'): bool
{
$logger = Kiri::getLogger();
if ($message instanceof Throwable) {
$logger->error($message, [$message]);
}
return $logger->addError($message, $method);
}
}
if (!function_exists('success')) {
/**
@@ -1186,7 +1209,7 @@ if (!function_exists('throwable')) {
*/
function throwable(\Throwable|\Error $throwable): string
{
$message = "\033[31mError: " . $throwable->getMessage() . "\033[0m" . PHP_EOL .
$message = "\033[31m" . $throwable->getMessage() . "\033[0m" . PHP_EOL .
' ' . $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL;
foreach ($throwable->getTrace() as $value) {
if (!isset($value['file'])) {
+1 -1
View File
@@ -207,7 +207,7 @@ abstract class Actor implements ActorInterface, JsonSerializable
try {
$this->onUpdate();
} catch (\Throwable $exception) {
\Kiri::getLogger()->error(throwable($exception));
error($exception);
}
Coroutine::sleep($this->refreshInterval / 1000);
+35 -32
View File
@@ -17,7 +17,7 @@ use ReflectionException;
*/
class Logger implements LoggerInterface
{
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
@@ -26,11 +26,11 @@ class Logger implements LoggerInterface
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
/**
* @param string $message
* @param array $context
@@ -42,8 +42,8 @@ class Logger implements LoggerInterface
// TODO: Implement emergency() method.
$this->log(Logger::EMERGENCY, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -55,8 +55,8 @@ class Logger implements LoggerInterface
// TODO: Implement alert() method.
$this->log(Logger::ALERT, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -68,8 +68,8 @@ class Logger implements LoggerInterface
// TODO: Implement critical() method.
$this->log(Logger::CRITICAL, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -79,8 +79,8 @@ class Logger implements LoggerInterface
// TODO: Implement error() method.
$this->log(Logger::ERROR, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -90,7 +90,7 @@ class Logger implements LoggerInterface
// TODO: Implement warning() method.
$this->log(Logger::WARNING, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -100,8 +100,8 @@ class Logger implements LoggerInterface
// TODO: Implement notice() method.
$this->log(Logger::NOTICE, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -111,8 +111,8 @@ class Logger implements LoggerInterface
// TODO: Implement info() method.
$this->log(Logger::INFO, $message, $context);
}
/**
* @param string $message
* @param array $context
@@ -122,8 +122,8 @@ class Logger implements LoggerInterface
// TODO: Implement debug() method.
$this->log(Logger::DEBUG, $message, $context);
}
/**
* @param mixed $level
* @param string $message
@@ -135,7 +135,7 @@ class Logger implements LoggerInterface
// TODO: Implement log() method.
$levels = Config::get('log.level', Logger::LOGGER_LEVELS);
if (in_array($level, $levels)) {
$_string = '[' . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL;
$_string = "[" . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL;
if (!empty($context)) {
$_string .= $this->_string($context);
}
@@ -143,14 +143,14 @@ class Logger implements LoggerInterface
return;
}
file_put_contents('php://output', $_string);
$filename = storage('log-' . date('Y-m-d') . '.log', 'log/');
file_put_contents($filename, $_string, FILE_APPEND);
}
}
/**
* @return void
* @throws Exception
@@ -159,8 +159,8 @@ class Logger implements LoggerInterface
{
$this->removeFile(storage());
}
/**
* @param string $dirname
* @return void
@@ -180,8 +180,8 @@ class Logger implements LoggerInterface
@unlink($path->getRealPath());
}
}
/**
* @param $context
* @return string
@@ -189,11 +189,14 @@ class Logger implements LoggerInterface
private function _string($context): string
{
if ($context instanceof \Throwable) {
$context = ['file' => $context->getFile(), 'line' => $context->getLine()];
$context = 'file -> ' . $context->getFile() . PHP_EOL . 'line -> ' . $context->getLine() . PHP_EOL;
}
if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) {
$context = ['file' => $context[0]->getFile(), 'line' => $context[0]->getLine()];
$context = 'file -> ' . $context[0]->getFile() . PHP_EOL . 'line -> ' . $context[0]->getLine() . PHP_EOL;
}
return print_r($context, true) . PHP_EOL;
if (is_string($context)) {
return $context . PHP_EOL;
}
return implode(PHP_EOL, $context) . PHP_EOL;
}
}
-1
View File
@@ -23,7 +23,6 @@ class StdoutLogger extends Logger
*/
public function addError($message, string $model = 'app'): bool
{
$this->error($model, [$message]);
if ($message instanceof \Exception) {
$this->errors[$model] = $message->getMessage();
} else {
+1 -1
View File
@@ -172,7 +172,7 @@ class Connection extends Component
$result = true;
}
} catch (Error|Throwable $exception) {
$result = \Kiri::getLogger()->addError($exception, 'mysql');
$result = addError($exception, 'mysql');
} finally {
return $result;
}
+1 -1
View File
@@ -186,7 +186,7 @@ SCRIPT;
try {
$response = $client->{$name}(...$arguments);
} catch (\Throwable $throwable) {
$response = \Kiri::getLogger()->addError($throwable->getMessage());
$response = addError($throwable, 'redis');
} finally {
$pool = Kiri::getDi()->get(Pool::class);
$pool->push($this->host, $client);