2022-02-23 16:32:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
2023-04-16 01:45:34 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-02-23 16:32:08 +08:00
|
|
|
namespace Kiri\Error;
|
|
|
|
|
|
2023-08-14 22:10:38 +08:00
|
|
|
use Kiri\Abstracts\Component;
|
2023-08-16 10:38:24 +08:00
|
|
|
use Monolog\Formatter\LineFormatter;
|
2023-08-16 10:32:53 +08:00
|
|
|
use Monolog\Handler\RotatingFileHandler;
|
2023-08-14 22:01:43 +08:00
|
|
|
use Monolog\Logger;
|
2023-08-14 21:09:45 +08:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use ReflectionException;
|
2022-02-23 16:32:08 +08:00
|
|
|
|
2023-08-14 22:01:43 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see LoggerInterface
|
2023-08-14 22:10:38 +08:00
|
|
|
* @method error(string $message, array $context)
|
|
|
|
|
* @method log($level, $message, array $context = array())
|
|
|
|
|
* @method debug($message, array $context = array())
|
|
|
|
|
* @method info($message, array $context = array())
|
|
|
|
|
* @method notice($message, array $context = array())
|
|
|
|
|
* @method warning($message, array $context = array())
|
|
|
|
|
* @method critical($message, array $context = array())
|
|
|
|
|
* @method alert($message, array $context = array())
|
|
|
|
|
* @method emergency($message, array $context = array())
|
2023-08-14 22:01:43 +08:00
|
|
|
*/
|
2023-08-14 22:10:38 +08:00
|
|
|
class StdoutLogger extends Component
|
2022-02-23 16:32:08 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2023-08-14 22:01:43 +08:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private array $errors = [];
|
|
|
|
|
|
|
|
|
|
|
2023-08-14 22:10:38 +08:00
|
|
|
/**
|
|
|
|
|
* @var Logger
|
|
|
|
|
*/
|
|
|
|
|
protected Logger $logger;
|
|
|
|
|
|
2023-08-14 22:59:53 +08:00
|
|
|
protected array $levels;
|
2023-08-14 22:10:38 +08:00
|
|
|
|
2023-08-15 16:06:50 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* StdoutLogger construct
|
|
|
|
|
*/
|
2023-08-14 22:59:53 +08:00
|
|
|
public function __construct()
|
2023-08-14 22:10:38 +08:00
|
|
|
{
|
2023-08-14 22:59:53 +08:00
|
|
|
parent::__construct();
|
|
|
|
|
|
2023-08-14 22:10:38 +08:00
|
|
|
$this->logger = new Logger(\config('id'));
|
2023-08-14 22:59:53 +08:00
|
|
|
$this->levels = [
|
|
|
|
|
'debug' => $this->logger::DEBUG,
|
|
|
|
|
'info' => $this->logger::INFO,
|
|
|
|
|
'notice' => $this->logger::NOTICE,
|
|
|
|
|
'warning' => $this->logger::WARNING,
|
|
|
|
|
'error' => $this->logger::ERROR,
|
|
|
|
|
'critical' => $this->logger::CRITICAL,
|
|
|
|
|
'alert' => $this->logger::ALERT,
|
|
|
|
|
'emergency' => $this->logger::EMERGENCY,
|
|
|
|
|
];
|
2023-08-14 22:10:38 +08:00
|
|
|
}
|
2022-02-23 16:32:08 +08:00
|
|
|
|
|
|
|
|
|
2023-08-14 21:09:45 +08:00
|
|
|
/**
|
|
|
|
|
* @param $message
|
|
|
|
|
* @param string $model
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-08-14 22:01:43 +08:00
|
|
|
public function failure($message, string $model = 'app'): bool
|
|
|
|
|
{
|
|
|
|
|
if ($message instanceof \Exception) {
|
|
|
|
|
$this->errors[$model] = $message->getMessage();
|
|
|
|
|
} else {
|
|
|
|
|
$this->errors[$model] = $message;
|
2023-07-31 23:09:00 +08:00
|
|
|
}
|
2023-08-16 12:32:42 +08:00
|
|
|
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . throwable($message) . PHP_EOL, FILE_APPEND);
|
2023-08-14 22:10:38 +08:00
|
|
|
$this->error(throwable($message), []);
|
2023-07-31 23:09:00 +08:00
|
|
|
return false;
|
2023-08-14 22:01:43 +08:00
|
|
|
}
|
2022-02-23 16:32:08 +08:00
|
|
|
|
|
|
|
|
|
2023-08-14 22:01:43 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $arguments
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function __call(string $name, array $arguments)
|
|
|
|
|
{
|
2023-08-14 22:59:53 +08:00
|
|
|
try {
|
|
|
|
|
if (method_exists($this->logger, $name)) {
|
|
|
|
|
$this->createHandler($name)->$name(...$arguments);
|
|
|
|
|
} else if (method_exists($this, $name)) {
|
|
|
|
|
$this->{$name}(...$arguments);
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
echo $exception->getMessage() . PHP_EOL;
|
2023-08-14 22:01:43 +08:00
|
|
|
}
|
2023-08-14 22:59:53 +08:00
|
|
|
}
|
2023-08-14 22:01:43 +08:00
|
|
|
|
2023-08-14 22:59:53 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return Logger
|
|
|
|
|
*/
|
|
|
|
|
protected function createHandler(string $name): Logger
|
|
|
|
|
{
|
|
|
|
|
if (!$this->logger->isHandling($this->levels[$name])) {
|
2023-08-16 15:01:45 +08:00
|
|
|
$handler = new RotatingFileHandler(APP_PATH . 'storage/logs/' . $name . '/kiri.log', $this->levels[$name]);
|
2023-08-16 10:38:59 +08:00
|
|
|
$handler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%:\n%message% %context%\n", 'Y-m-d H:i:s'));
|
2023-08-16 10:38:24 +08:00
|
|
|
$this->logger->pushHandler($handler);
|
2023-08-14 22:01:43 +08:00
|
|
|
}
|
2023-08-14 22:59:53 +08:00
|
|
|
return $this->logger;
|
2023-08-14 22:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $model
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getLastError(string $model = 'app'): mixed
|
|
|
|
|
{
|
|
|
|
|
return $this->errors[$model] ?? 'Unknown error.';
|
|
|
|
|
}
|
2022-02-23 16:32:08 +08:00
|
|
|
|
|
|
|
|
}
|