From d682b5f2761746fc8b448b3a614c6eddf456ff6e Mon Sep 17 00:00:00 2001 From: Andrii Riabchenko Date: Fri, 25 Apr 2025 14:34:48 +0300 Subject: [PATCH 1/2] Add logging functionality with Monolog and create log directory --- composer.json | 3 ++- composer.lock | 22 +++++++++++----------- config/directories.php | 1 + core/library/log.php | 35 +++++++++++++++++++++++++++++++++++ logs/.gitkeep | 0 5 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 core/library/log.php create mode 100644 logs/.gitkeep diff --git a/composer.json b/composer.json index a41a962..5b56540 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "require": { "twig/twig": "^3.9", "vlucas/phpdotenv": "^5.6", - "google/apiclient": "^2.0" + "google/apiclient": "^2.0", + "monolog/monolog": "^3.9" }, "config": { "vendor-dir": "core/vendor" diff --git a/composer.lock b/composer.lock index 201afa3..006070e 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": "dea1799a33b980a172ae976e82b33dab", + "content-hash": "60ec694ca36984f88a96737c757f01cd", "packages": [ { "name": "firebase/php-jwt", @@ -632,16 +632,16 @@ }, { "name": "monolog/monolog", - "version": "3.8.1", + "version": "3.9.0", "source": { "type": "git", "url": "https://github.com/Seldaek/monolog.git", - "reference": "aef6ee73a77a66e404dd6540934a9ef1b3c855b4" + "reference": "10d85740180ecba7896c87e06a166e0c95a0e3b6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/monolog/zipball/aef6ee73a77a66e404dd6540934a9ef1b3c855b4", - "reference": "aef6ee73a77a66e404dd6540934a9ef1b3c855b4", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/10d85740180ecba7896c87e06a166e0c95a0e3b6", + "reference": "10d85740180ecba7896c87e06a166e0c95a0e3b6", "shasum": "" }, "require": { @@ -719,7 +719,7 @@ ], "support": { "issues": "https://github.com/Seldaek/monolog/issues", - "source": "https://github.com/Seldaek/monolog/tree/3.8.1" + "source": "https://github.com/Seldaek/monolog/tree/3.9.0" }, "funding": [ { @@ -731,7 +731,7 @@ "type": "tidelift" } ], - "time": "2024-12-05T17:15:07+00:00" + "time": "2025-03-24T10:02:05+00:00" }, { "name": "paragonie/constant_time_encoding", @@ -1811,10 +1811,10 @@ "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, - "platform": [], - "platform-dev": [], - "plugin-api-version": "2.3.0" + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.6.0" } diff --git a/config/directories.php b/config/directories.php index 86b5738..1471484 100644 --- a/config/directories.php +++ b/config/directories.php @@ -9,6 +9,7 @@ define('DIR_STATIC', DIR_ROOT . '/static/'); define('DIR_WWW', DIR_ROOT . '/www/'); define('DIR_DATA', DIR_ROOT . '/data/'); +define('DIR_LOG', DIR_ROOT . '/logs/'); // Define the subdirectories define('DIR_SESSION', DIR_DATA . 'session/'); diff --git a/core/library/log.php b/core/library/log.php new file mode 100644 index 0000000..4ff5cc1 --- /dev/null +++ b/core/library/log.php @@ -0,0 +1,35 @@ +logger = new Logger($channelName); + + $this->logger->pushHandler(new StreamHandler(DIR_LOG . $logFile, Logger::DEBUG)); + + $logDir = DIR_LOG; + if (!is_dir($logDir)) { + mkdir($logDir, 0777, true); + } + } + + public function info($message, array $context = []) { + $this->logger->info($message, $context); + } + + public function error($message, array $context = []) { + $this->logger->error($message, $context); + } + + public function warning($message, array $context = []) { + $this->logger->warning($message, $context); + } + + public function log($level, $message, array $context = []) { + $this->logger->log($level, $message, $context); + } +} \ No newline at end of file diff --git a/logs/.gitkeep b/logs/.gitkeep new file mode 100644 index 0000000..e69de29 From 1ce5620f7679493731e71f8061df038ad20caa84 Mon Sep 17 00:00:00 2001 From: Andrii Riabchenko Date: Fri, 25 Apr 2025 14:38:48 +0300 Subject: [PATCH 2/2] Implement error and exception logging in bootstrap and log classes --- core/bootstrap.php | 14 ++++++++++++++ core/library/log.php | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/core/bootstrap.php b/core/bootstrap.php index f151c18..e7d1ef3 100644 --- a/core/bootstrap.php +++ b/core/bootstrap.php @@ -6,6 +6,20 @@ $registry = new Registry(); +$registry->set('phplog', new Log('phpapp', 'php.log')); + +set_error_handler(function ($errno, $errstr, $errfile, $errline) use ($log) { + $log->error("PHP Error: [$errno] $errstr in $errfile on line $errline"); + return false; +}); + +set_exception_handler(function ($exception) use ($log) { + $log->exception($exception); +}); + +ini_set('log_errors', '1'); +ini_set('error_log', DIR_LOG . 'php_errors.log'); + $registry->set('env', new Env()); $registry->set('request', new Request()); $registry->set('load', new Load($registry)); diff --git a/core/library/log.php b/core/library/log.php index 4ff5cc1..46631f9 100644 --- a/core/library/log.php +++ b/core/library/log.php @@ -32,4 +32,8 @@ public function warning($message, array $context = []) { public function log($level, $message, array $context = []) { $this->logger->log($level, $message, $context); } + + public function exception(\Throwable $exception) { + $this->logger->error($exception->getMessage(), ['exception' => $exception]); + } } \ No newline at end of file