Revert "Revert "Add logging & some better error handling""

This reverts commit a1ac9edadc.
This commit is contained in:
Lewis Dale committed 2023-08-26 19:11:30 +01:00
1 parent a1ac9edadc
commit be8d37d4cb
5 files changed
+91 -6

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);
namespace ImageResizer\Lib;
use DateTime;
use Psr\Log\AbstractLogger;
use Stringable;
class FileLogger extends AbstractLogger
{
function __construct(private readonly string $filename = 'php://stdout') {}
public function log($level, Stringable|string $message, array $context = []): void
{
$timestamp = new DateTime();
$callingClass = debug_backtrace()[2]['class'];
$callingFunction = debug_backtrace()[2]['function'];
$log = $timestamp->format(DATE_ATOM) . " [$level] $callingClass::$callingFunction $message " . json_encode($context);
fwrite(fopen($this->filename, 'w'), $log . PHP_EOL);
}
}
+7 -2
View File
@@ -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);