From 62221d04cdaa8cf73a1c79562f3fa8afd95698e7 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Sat, 26 Aug 2023 19:09:27 +0100 Subject: [PATCH] Add logging & some better error handling --- composer.json | 3 ++- composer.lock | 52 +++++++++++++++++++++++++++++++++++++++++- src/ImageResizer.php | 12 ++++++++-- src/Lib/FileLogger.php | 21 +++++++++++++++++ src/Lib/Resizer.php | 9 ++++++-- 5 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 src/Lib/FileLogger.php diff --git a/composer.json b/composer.json index 2576eb5..692f5aa 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "vlucas/phpdotenv": "^5.5", "ext-gd": "*", "ext-pdo": "*", - "ext-pdo_sqlite": "*" + "ext-pdo_sqlite": "*", + "psr/log": "^3.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index bdf0ccf..4a86e59 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": "35cc1fc18887fe14e702beb8a8486527", + "content-hash": "2269f5deec22d5f78ed3c229cddb1359", "packages": [ { "name": "graham-campbell/result-type", @@ -384,6 +384,56 @@ }, "time": "2021-11-05T16:47:00+00:00" }, + { + "name": "psr/log", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/3.0.0" + }, + "time": "2021-07-14T16:46:02+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.27.0", diff --git a/src/ImageResizer.php b/src/ImageResizer.php index 8c99779..74a099a 100644 --- a/src/ImageResizer.php +++ b/src/ImageResizer.php @@ -3,10 +3,12 @@ error_reporting(E_ERROR | E_PARSE); require_once './vendor/autoload.php'; +use ImageResizer\Lib\FileLogger; use ImageResizer\Lib\Resizer; use ImageResizer\Lib\SqliteImageDb; use ImageResizer\Models\ResizeParams; +$logger = new FileLogger("resizer.log"); $dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]); $dotenv->load(); @@ -15,6 +17,12 @@ $imageDb = new SqliteImageDb(new PDO("sqlite:{$_ENV["db_name"]}")); $imgPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $params = new ResizeParams($_GET); -$resizer = new Resizer($imageDb); +$resizer = new Resizer($imageDb, $logger); -echo $resizer->performResize($imgPath, $params); \ No newline at end of file +try { + $resizer->performResize($imgPath, $params); +} catch (\Exception $e) { + $logger->error($e->getMessage()); + http_response_code(500); + echo "Internal Server Error"; +} \ No newline at end of file diff --git a/src/Lib/FileLogger.php b/src/Lib/FileLogger.php new file mode 100644 index 0000000..544e393 --- /dev/null +++ b/src/Lib/FileLogger.php @@ -0,0 +1,21 @@ +format(DATE_ATOM) . " [$level] $callingClass::$callingFunction $message " . json_encode($context); + fwrite(fopen($this->filename, 'w'), $log . PHP_EOL); + } +} + diff --git a/src/Lib/Resizer.php b/src/Lib/Resizer.php index 959cc75..52bc0f0 100644 --- a/src/Lib/Resizer.php +++ b/src/Lib/Resizer.php @@ -6,10 +6,13 @@ namespace ImageResizer\Lib; use ImageResizer\Models\ResizeParams; use ImageResizer\Models\Image; +use Psr\Log\AbstractLogger; + class Resizer { public function __construct( - private readonly ImageDb $imageDb + private readonly ImageDb $imageDb, + private readonly AbstractLogger $logger, ) {} @@ -20,7 +23,7 @@ class Resizer { // Check if the image has already been processed if ($image = $this->imageDb->findImage($path, $params)) { - + $this->logger->info("Found existing image", ['path'=>$path, 'params'=>$params]); header("Content-type: {${$image->getMime()}}"); return base64_decode($image->encode(75)); } @@ -28,6 +31,7 @@ class Resizer // Fetch the image at the given URL $original_img = Image::fromUrl($_ENV['ROOT_DOMAIN'] . $path); $converted = $this->createVariant($original_img, $params); + $this->logger->info("Created variant", ['path'=>$path, 'params'=>$params]); // Store the image in the database $this->imageDb->saveImage( @@ -35,6 +39,7 @@ class Resizer $params, $converted ); + $this->logger->info("Saved image for $path"); header("Content-type: {$original_img->getMime()}"); return base64_decode($converted);