Files

155 lines
3.7 KiB
PHP
Raw Permalink Normal View History

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;
2025-12-31 00:19:31 +08:00
use Throwable;
2022-02-23 16:32:08 +08:00
2023-08-14 22:01:43 +08:00
/**
* @see LoggerInterface
2023-08-24 13:49:58 +08:00
* @method error(string $message, array $context = array())
2023-08-14 22:10:38 +08:00
* @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();
2025-12-18 15:39:47 +08:00
$this->logger = new Logger(\config('site.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
*/
2025-12-31 00:19:31 +08:00
public function logCategory($message, string $model = 'app'): bool
2023-08-14 22:01:43 +08:00
{
if ($message instanceof \Exception) {
$this->errors[$model] = $message->getMessage();
} else {
$this->errors[$model] = $message;
2023-07-31 23:09:00 +08:00
}
2025-12-31 00:19:31 +08:00
$this->println($message);
return false;
2023-08-18 20:56:45 +08:00
}
2025-12-31 00:19:31 +08:00
/**
* @param Throwable $exception
* @param array $data
* @param mixed|null $result
* @return bool
*/
public function json_log(Throwable $exception, array $data = [], mixed $result = null): mixed
{
json_log($exception, $data);
$this->println($exception->getMessage());
return $result;
}
2022-02-23 16:32:08 +08:00
2024-01-10 17:49:50 +08:00
/**
* @param string $message
* @return void
*/
public function println(string $message): void
{
2024-01-10 17:52:18 +08:00
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $message . PHP_EOL, FILE_APPEND);
2024-01-10 17:49:50 +08:00
}
2023-08-14 22:01:43 +08:00
/**
* @param string $name
* @param array $arguments
* @return void
2023-12-12 15:35:38 +08:00
* @throws
2023-08-14 22:01:43 +08:00
*/
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);
}
2025-12-31 00:19:31 +08:00
} catch (Throwable $exception) {
$this->println($exception->getMessage());
$this->json_log($exception);
}
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-24 16:22:09 +08:00
$handler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %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
}