This commit is contained in:
2023-08-14 22:01:43 +08:00
parent 4850748c77
commit 676f0ce1e6
2 changed files with 58 additions and 26 deletions
@@ -17,7 +17,6 @@ use Kiri\Events\EventInterface;
use Kiri\Di\LocalService; use Kiri\Di\LocalService;
use Kiri\Config\ConfigProvider; use Kiri\Config\ConfigProvider;
use Kiri\Exception\{InitException}; use Kiri\Exception\{InitException};
use Monolog\Handler\StreamHandler;
use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
@@ -90,12 +89,6 @@ abstract class BaseApplication extends Component
public function mapping(ConfigProvider $config): void public function mapping(ConfigProvider $config): void
{ {
$this->container->bind(LoggerInterface::class, $logger = new Logger($config->get('id'))); $this->container->bind(LoggerInterface::class, $logger = new Logger($config->get('id')));
$levels = \config('log.level', self::LOGGER_LEVELS);
foreach ($levels as $level) {
$logger->pushHandler(new StreamHandler(APP_PATH . 'storage/logs/' . Logger::getLevelName($level) . '/' . date('Y-m-d') . '.log', $level));
}
foreach ($config->get('mapping', []) as $interface => $class) { foreach ($config->get('mapping', []) as $interface => $class) {
$this->container->set($interface, $class); $this->container->set($interface, $class);
} }
+58 -19
View File
@@ -4,17 +4,30 @@ declare(strict_types=1);
namespace Kiri\Error; namespace Kiri\Error;
use Kiri\Abstracts\BaseApplication;
use Kiri\Application;
use Kiri\Di\Inject\Container;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
/**
* @see LoggerInterface
*/
class StdoutLogger class StdoutLogger
{ {
/** /**
* @var array * @var array
*/ */
private array $errors = []; private array $errors = [];
#[Container(Logger::class)]
public Logger $logger;
/** /**
@@ -23,26 +36,52 @@ class StdoutLogger
* @return bool * @return bool
* @throws ReflectionException * @throws ReflectionException
*/ */
public function failure($message, string $model = 'app'): bool public function failure($message, string $model = 'app'): bool
{ {
if ($message instanceof \Exception) { if ($message instanceof \Exception) {
$this->errors[$model] = $message->getMessage(); $this->errors[$model] = $message->getMessage();
} else { } else {
$this->errors[$model] = $message; $this->errors[$model] = $message;
} }
$logger = \Kiri::getDi()->get(LoggerInterface::class); $logger = \Kiri::getDi()->get(LoggerInterface::class);
$logger->error(throwable($message), []); $logger->error(throwable($message), []);
return false; return false;
} }
/** /**
* @param string $model * @param string $name
* @return mixed * @param array $arguments
*/ * @return void
public function getLastError(string $model = 'app'): mixed * @throws ReflectionException
{ */
return $this->errors[$model] ?? 'Unknown error.'; public function __call(string $name, array $arguments)
} {
// TODO: Implement __call() method.
if (!isset($arguments[2])) {
$arguments[2] = [];
}
[$level, $message, $context] = $arguments;
$levels = \config('log.level', BaseApplication::LOGGER_LEVELS);
if (!in_array($level, $levels)) {
return;
}
if (!$this->logger->isHandling($level)) {
$path = APP_PATH . 'storage/logs/' . strtolower(Logger::getLevelName($level)) . '/' . date('Y-m-d') . '.log';
$this->logger->pushHandler(new StreamHandler($path, $level));
}
$this->logger->{$name}($level, $message, $context);
}
/**
* @param string $model
* @return mixed
*/
public function getLastError(string $model = 'app'): mixed
{
return $this->errors[$model] ?? 'Unknown error.';
}
} }