Adds loggin

This commit is contained in:
Michel 2024-11-07 22:05:24 +01:00
parent 41a12d9a82
commit 26e11b5a51
10 changed files with 156 additions and 5 deletions

View file

@ -37,5 +37,7 @@ final class ContainerHandler
$reflectionContainer = new ReflectionContainer(true);
self::$instance->delegate($reflectionContainer);
self::$instance->addShared(Container::class, self::$instance);
}
}

View file

@ -4,10 +4,14 @@ namespace GamesShop;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Templates\TemplateEngine;
use League\Container\Container;
use Monolog\Logger;
use Throwable;
use Whoops\Handler\CallbackHandler;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Inspector\InspectorInterface;
use Whoops\Run;
use Whoops\RunInterface;
final class CrashHandler
{
@ -32,7 +36,7 @@ final class CrashHandler
}
return new CallbackHandler(
function ($exception, $inspector, $run) {
function (Throwable $exception, InspectorInterface $inspector, RunInterface $run) {
http_response_code(500);
echo ContainerHandler::get(TemplateEngine::class)
->renderErrorPage(500);

View file

@ -42,4 +42,8 @@ final class EnvironmentHandler
{
return $_SERVER['USE_SSL'] === 'true';
}
public function getLoggingPath(): string {
return $_SERVER['LOG_PATH'] ?? '';
}
}

View file

@ -4,6 +4,7 @@ namespace GamesShop\Errors;
use Exception;
use GamesShop\ContainerHandler;
use Monolog\Logger;
use Whoops\Handler\HandlerInterface;
use Whoops\Handler\PrettyPageHandler;
@ -14,6 +15,12 @@ final class ExtendedException extends Exception
public function __construct(string $message = "", array $additionals = [], int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
ContainerHandler::get(Logger::class)
->error(
$message,
$additionals
);
$handler = ContainerHandler::get(HandlerInterface::class);

25
src/php/Services.php Normal file
View file

@ -0,0 +1,25 @@
<?php declare(strict_types=1);
namespace GamesShop;
use GamesShop\Environment\EnvironmentHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
final class Services
{
public static function createLogger(): void
{
$logPath = ContainerHandler::get(EnvironmentHandler::class)->getLoggingPath();
$logger = new Logger('gamesshop');
$logger->pushHandler(new StreamHandler($logPath, Level::Debug));
ContainerHandler::getInstance()
->addShared(LoggerInterface::class, $logger)
->setAlias(Logger::class);
}
}

View file

@ -4,8 +4,10 @@ declare(strict_types=1);
use GamesShop\ContainerHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Services;
require_once __DIR__ . '/../../vendor/autoload.php';
ContainerHandler::get(EnvironmentHandler::class)->load();
ContainerHandler::get(DoctrineManager::class)->setup();
ContainerHandler::get(DoctrineManager::class)->setup();
Services::createLogger();

View file

@ -5,6 +5,7 @@ use GamesShop\ContainerHandler;
use GamesShop\CrashHandler;
use GamesShop\DoctrineManager;
use GamesShop\Environment\EnvironmentHandler;
use GamesShop\Errors\ExtendedException;
use GamesShop\Routing\Router;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Whoops\Handler\HandlerInterface;
@ -15,6 +16,8 @@ require_once __DIR__ . '/../src/php/bootstrap.php';
ContainerHandler::get(CrashHandler::class)->register();
throw new ExtendedException('Test', [ 'gell', 'Hub' ]);
$router = ContainerHandler::getInstance()->get(Router::class);
$result = $router->route();