From 26e11b5a51a76fc7c1ea6cf748a5a03edd123b2d Mon Sep 17 00:00:00 2001 From: Michel Date: Thu, 7 Nov 2024 22:05:24 +0100 Subject: [PATCH] Adds loggin --- composer.json | 3 +- composer.lock | 103 ++++++++++++++++++++- config/.env.example | 4 +- src/php/ContainerHandler.php | 2 + src/php/CrashHandler.php | 6 +- src/php/Environment/EnvironmentHandler.php | 4 + src/php/Errors/ExtendedException.php | 7 ++ src/php/Services.php | 25 +++++ src/php/bootstrap.php | 4 +- src/php/index.php | 3 + 10 files changed, 156 insertions(+), 5 deletions(-) create mode 100644 src/php/Services.php diff --git a/composer.json b/composer.json index 77eddf5..434801b 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "php-curl-class/php-curl-class": "^9.19", "symfony/cache": "^7.1", "phpoffice/phpspreadsheet": "^2.1", - "symfony/polyfill-iconv": "^1.31" + "symfony/polyfill-iconv": "^1.31", + "monolog/monolog": "^3.7" } } diff --git a/composer.lock b/composer.lock index e6ff606..52fa9c8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "05654f94aa961d630f571a3d533e8543", + "content-hash": "bb114d3310ab85f9eb6fab629f30f427", "packages": [ { "name": "doctrine/collections", @@ -1524,6 +1524,107 @@ }, "time": "2022-12-02T22:17:43+00:00" }, + { + "name": "monolog/monolog", + "version": "3.7.0", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "f4393b648b78a5408747de94fca38beb5f7e9ef8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/f4393b648b78a5408747de94fca38beb5f7e9ef8", + "reference": "f4393b648b78a5408747de94fca38beb5f7e9ef8", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "psr/log": "^2.0 || ^3.0" + }, + "provide": { + "psr/log-implementation": "3.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^3.0", + "doctrine/couchdb": "~1.0@dev", + "elasticsearch/elasticsearch": "^7 || ^8", + "ext-json": "*", + "graylog2/gelf-php": "^1.4.2 || ^2.0", + "guzzlehttp/guzzle": "^7.4.5", + "guzzlehttp/psr7": "^2.2", + "mongodb/mongodb": "^1.8", + "php-amqplib/php-amqplib": "~2.4 || ^3", + "phpstan/phpstan": "^1.9", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-strict-rules": "^1.4", + "phpunit/phpunit": "^10.5.17", + "predis/predis": "^1.1 || ^2", + "ruflin/elastica": "^7", + "symfony/mailer": "^5.4 || ^6", + "symfony/mime": "^5.4 || ^6" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-curl": "Required to send log messages using the IFTTTHandler, the LogglyHandler, the SendGridHandler, the SlackWebhookHandler or the TelegramBotHandler", + "ext-mbstring": "Allow to work properly with unicode symbols", + "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", + "ext-openssl": "Required to send log messages using SSL", + "ext-sockets": "Allow sending log messages to a Syslog server (via UDP driver)", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "https://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "https://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "support": { + "issues": "https://github.com/Seldaek/monolog/issues", + "source": "https://github.com/Seldaek/monolog/tree/3.7.0" + }, + "funding": [ + { + "url": "https://github.com/Seldaek", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/monolog/monolog", + "type": "tidelift" + } + ], + "time": "2024-06-28T09:40:51+00:00" + }, { "name": "nikic/fast-route", "version": "v1.3.0", diff --git a/config/.env.example b/config/.env.example index 57c91ec..9e264f9 100644 --- a/config/.env.example +++ b/config/.env.example @@ -6,4 +6,6 @@ DISCORD_CLIENT_SECRET="" DISCORD_CLIENT_LOGIN_URI="" DB_DRIVER="pdo_sqlite" -DB_PATH="/app/data/db.sqlite" \ No newline at end of file +DB_PATH="/app/data/db.sqlite" + +LOG_PATH="/app/data/php.log" \ No newline at end of file diff --git a/src/php/ContainerHandler.php b/src/php/ContainerHandler.php index dca8d30..f4b284b 100644 --- a/src/php/ContainerHandler.php +++ b/src/php/ContainerHandler.php @@ -37,5 +37,7 @@ final class ContainerHandler $reflectionContainer = new ReflectionContainer(true); self::$instance->delegate($reflectionContainer); self::$instance->addShared(Container::class, self::$instance); + + } } \ No newline at end of file diff --git a/src/php/CrashHandler.php b/src/php/CrashHandler.php index dd4aa47..527acaa 100644 --- a/src/php/CrashHandler.php +++ b/src/php/CrashHandler.php @@ -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); diff --git a/src/php/Environment/EnvironmentHandler.php b/src/php/Environment/EnvironmentHandler.php index 279e36b..0cffafc 100644 --- a/src/php/Environment/EnvironmentHandler.php +++ b/src/php/Environment/EnvironmentHandler.php @@ -42,4 +42,8 @@ final class EnvironmentHandler { return $_SERVER['USE_SSL'] === 'true'; } + + public function getLoggingPath(): string { + return $_SERVER['LOG_PATH'] ?? ''; + } } \ No newline at end of file diff --git a/src/php/Errors/ExtendedException.php b/src/php/Errors/ExtendedException.php index 745d54a..82f085d 100644 --- a/src/php/Errors/ExtendedException.php +++ b/src/php/Errors/ExtendedException.php @@ -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); diff --git a/src/php/Services.php b/src/php/Services.php new file mode 100644 index 0000000..5d67a09 --- /dev/null +++ b/src/php/Services.php @@ -0,0 +1,25 @@ +getLoggingPath(); + + $logger = new Logger('gamesshop'); + $logger->pushHandler(new StreamHandler($logPath, Level::Debug)); + + ContainerHandler::getInstance() + ->addShared(LoggerInterface::class, $logger) + ->setAlias(Logger::class); + } +} \ No newline at end of file diff --git a/src/php/bootstrap.php b/src/php/bootstrap.php index c233340..0025278 100644 --- a/src/php/bootstrap.php +++ b/src/php/bootstrap.php @@ -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(); \ No newline at end of file +ContainerHandler::get(DoctrineManager::class)->setup(); +Services::createLogger(); \ No newline at end of file diff --git a/src/php/index.php b/src/php/index.php index 4857464..6eacf7d 100644 --- a/src/php/index.php +++ b/src/php/index.php @@ -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(); -- 2.45.2