This commit is contained in:
2023-08-14 21:09:45 +08:00
parent 754998830c
commit 513690e9a3
5 changed files with 38 additions and 16 deletions
+3 -1
View File
@@ -29,7 +29,9 @@
"ext-sockets": "*", "ext-sockets": "*",
"nikic/php-parser": "^4.15", "nikic/php-parser": "^4.15",
"ext-inotify": "*", "ext-inotify": "*",
"ext-ssh2": "*" "ext-ssh2": "*",
"monolog/monolog": "^2.9",
"psr/container": "^2.0"
}, },
"replace": { "replace": {
"symfony/polyfill-apcu": "*", "symfony/polyfill-apcu": "*",
+13 -2
View File
@@ -16,14 +16,15 @@ use Kiri;
use Kiri\Events\EventInterface; use Kiri\Events\EventInterface;
use Kiri\Di\LocalService; use Kiri\Di\LocalService;
use Kiri\Config\ConfigProvider; use Kiri\Config\ConfigProvider;
use Kiri\Error\StdoutLogger;
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;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use ReflectionException; use ReflectionException;
use Monolog\Logger;
/** /**
* Class BaseApplication * Class BaseApplication
@@ -75,17 +76,27 @@ abstract class BaseApplication extends Component
$this->mapping($config); $this->mapping($config);
$this->parseStorage($config); $this->parseStorage($config);
$this->parseEvents($config); $this->parseEvents($config);
parent::__construct(); parent::__construct();
} }
const LOGGER_LEVELS = [Logger::EMERGENCY, Logger::ALERT, Logger::CRITICAL, Logger::ERROR, Logger::WARNING, Logger::NOTICE, Logger::INFO, Logger::DEBUG];
/** /**
* @param ConfigProvider $config * @param ConfigProvider $config
* @return void * @return void
*/ */
public function mapping(ConfigProvider $config): void public function mapping(ConfigProvider $config): void
{ {
$this->container->set(LoggerInterface::class, StdoutLogger::class); $this->container->bind(LoggerInterface::class, $logger = new Logger($config->get('id')));
$levels = \config('log.level', self::LOGGER_LEVELS);
foreach ($levels as $level) {
$run = new StreamHandler(APP_PATH . 'storage/logs/' . $level . '/' . date('Y-m-d') . '.log', $level);
$logger->pushHandler($run);
}
foreach ($config->get('mapping', []) as $interface => $class) { foreach ($config->get('mapping', []) as $interface => $class) {
$this->container->set($interface, $class); $this->container->set($interface, $class);
} }
+11 -4
View File
@@ -4,13 +4,10 @@ namespace Kiri\Abstracts;
use DirectoryIterator; use DirectoryIterator;
use Exception; use Exception;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri; use Kiri;
use Kiri\Server\Events\OnWorkerStop; use Monolog\Handler\StreamHandler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ReflectionException; use ReflectionException;
use Symfony\Component\Console\Output\OutputInterface;
/** /**
@@ -168,6 +165,16 @@ class Logger implements LoggerInterface
*/ */
public function log($level, $message, array $context = []): void public function log($level, $message, array $context = []): void
{ {
/** @var \Monolog\Logger $logger */
$logger = Kiri::getDi()->get(LoggerInterface::class);
$logger->log($level, $message, $context);
$run = new StreamHandler(APP_PATH . 'storage/logs/' . date('Y-m-d') . '/'.date('Y-m-d').'.log');
$logger->pushHandler($run);
if (!in_array($level, $this->levels)) return; if (!in_array($level, $this->levels)) return;
$_string = "[" . now() . ']: ' . $message; $_string = "[" . now() . ']: ' . $message;
if (!empty($context)) { if (!empty($context)) {
-1
View File
@@ -17,7 +17,6 @@ use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface; use Psr\Container\NotFoundExceptionInterface;
use ReflectionException; use ReflectionException;
use Kiri\Abstracts\Logger;
use Kiri\Events\OnSystemError; use Kiri\Events\OnSystemError;
/** /**
+6 -3
View File
@@ -4,9 +4,10 @@ declare(strict_types=1);
namespace Kiri\Error; namespace Kiri\Error;
use Kiri\Abstracts\Logger; use Psr\Log\LoggerInterface;
use ReflectionException;
class StdoutLogger extends Logger class StdoutLogger
{ {
@@ -20,6 +21,7 @@ class StdoutLogger extends Logger
* @param $message * @param $message
* @param string $model * @param string $model
* @return bool * @return bool
* @throws ReflectionException
*/ */
public function failure($message, string $model = 'app'): bool public function failure($message, string $model = 'app'): bool
{ {
@@ -28,7 +30,8 @@ class StdoutLogger extends Logger
} else { } else {
$this->errors[$model] = $message; $this->errors[$model] = $message;
} }
$this->error(throwable($message), []); $logger = \Kiri::getDi()->get(LoggerInterface::class);
$logger->error(throwable($message), []);
return false; return false;
} }