Skip to content

Commit c52a10e

Browse files
authored
feat: logging (#5)
1 parent d2f1ab4 commit c52a10e

File tree

13 files changed

+123
-15
lines changed

13 files changed

+123
-15
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ APP_DEBUG=0
33
# Cache (in seconds)
44
CACHE_TTL=31536000
55

6+
# Logging
7+
LOG_STREAM=php://stderr
8+
LOG_LEVEL=debug
9+
610
# Allowed domains (seperate by comma)
711
ALLOWED_DOMAINS=example.com
812

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Edit .env.local to match your setup:
7272
```
7373
APP_DEBUG=0
7474
75-
ALLOWED_DOMAINS=https://www.mysite.com,https://www.another-site.com
75+
ALLOWED_DOMAINS=mysite.com,another-site.com
7676
STORAGE_TYPE=local # "local" or "s3"
7777
7878
# Local storage configuration
@@ -86,7 +86,11 @@ S3_ACCESS_KEY=your-access-key
8686
S3_SECRET_KEY=your-secret-key
8787
8888
# Cache (in seconds)
89-
CACHE_TTL=3600
89+
CACHE_TTL=31536000
90+
91+
# Logging
92+
LOG_STREAM=/srv/.cache/log/cdn-php.log
93+
LOG_LEVEL=debug
9094
```
9195

9296
## Running with Docker

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"babeuloula/phpunit-coverage-checker": "^1.0",
99
"bref/bref": "^2.3",
1010
"bref/extra-php-extensions": "^1.7",
11+
"bref/logger": "^1.0",
1112
"league/flysystem": "^3.29",
1213
"league/flysystem-aws-s3-v3": "^3.0",
1314
"league/glide": "^2.3",

composer.lock

Lines changed: 57 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpmd-ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
</rule>
5050
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
5151
<properties>
52-
<property name="maximum" value="15"/>
52+
<property name="maximum" value="20"/>
5353
</properties>
5454
</rule>
5555
<rule ref="rulesets/naming.xml">

serverless.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ params:
88
prod:
99
bucket_prefix: ''
1010
app_debug: 0
11+
log_level: info
1112

1213
dev:
1314
bucket_prefix: 'dev-'
1415
app_debug: 1
16+
log_level: debug
1517

1618
provider:
1719
name: aws
@@ -45,6 +47,8 @@ functions:
4547
S3_REGION: ${ssm:/cdn-php/s3-region}
4648
S3_ACCESS_KEY: ${ssm:/cdn-php/s3-access-key}
4749
S3_SECRET_KEY: ${ssm:/cdn-php/s3-secret-key}
50+
LOG_STREAM: php://stderr
51+
LOG_LEVEL: ${param:log_level}
4852

4953
package:
5054
patterns:

src/Cdn.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use BaBeuloula\CdnPhp\Processor\ImageProcessor;
2424
use BaBeuloula\CdnPhp\Processor\PathProcessor;
2525
use BaBeuloula\CdnPhp\Storage\Storage;
26+
use Psr\Log\LoggerInterface;
2627
use Symfony\Component\HttpFoundation\Request;
2728
use Symfony\Component\HttpFoundation\Response;
2829

@@ -33,7 +34,8 @@ public function __construct(
3334
private readonly array $allowedDomains,
3435
private readonly Storage $storage,
3536
private readonly ImageProcessor $imageProcessor,
36-
private readonly Cache $cache
37+
private readonly Cache $cache,
38+
private readonly LoggerInterface $logger,
3739
) {
3840
}
3941

@@ -72,6 +74,8 @@ public function handleRequest(Request $request): Response
7274
$this->storage->save($cachedPath, $this->storage->read($processedImage));
7375
}
7476

77+
$this->logger->info('Serve the image: {cachedPath}', ['cachedPath' => $cachedPath]);
78+
7579
return $this->cache->createResponse($cachedPath, $supportWebp, $request);
7680
}
7781

src/ContainerConfig.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
use BaBeuloula\CdnPhp\Flysystem\Adapter\UrlFilesystemAdapter;
1919
use BaBeuloula\CdnPhp\Processor\ImageProcessor;
2020
use BaBeuloula\CdnPhp\Storage\Storage;
21+
use Bref\Logger\StderrLogger as BrefLogger;
2122
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
2223
use League\Flysystem\AwsS3V3\PortableVisibilityConverter;
2324
use League\Flysystem\Filesystem as LeagueFilesystem;
2425
use League\Flysystem\FilesystemAdapter;
2526
use League\Flysystem\Local\LocalFilesystemAdapter;
2627
use League\Flysystem\Visibility;
2728
use Pimple\Container;
29+
use Psr\Log\LoggerInterface;
2830
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
2931

3032
final class ContainerConfig extends Container
@@ -38,6 +40,11 @@ public function __construct()
3840
$this['cache_ttl'] = (int) getenv('CACHE_TTL');
3941
$this['allowed_domains'] = explode(',', (string) getenv('ALLOWED_DOMAINS'));
4042

43+
$this[LoggerInterface::class] = static fn () => new BrefLogger(
44+
(string) getenv('LOG_LEVEL'),
45+
(string) getenv('LOG_STREAM')
46+
);
47+
4148
$this[SymfonyFilesystem::class] = static fn() => new SymfonyFilesystem();
4249

4350
switch ($this['storage_driver']) {
@@ -76,12 +83,14 @@ public function __construct()
7683
$this[LeagueFilesystem::class] = static fn(self $c) => new LeagueFilesystem($c[FilesystemAdapter::class]);
7784
$this[Storage::class] = static fn (self $c) => new Storage(
7885
$c[LeagueFilesystem::class],
79-
$c[SymfonyFilesystem::class]
86+
$c[SymfonyFilesystem::class],
87+
$c[LoggerInterface::class],
8088
);
8189

8290
$this[ImageProcessor::class] = static fn(self $c) => new ImageProcessor(
8391
$c[FilesystemAdapter::class],
84-
$c[UrlFilesystemAdapter::class]
92+
$c[UrlFilesystemAdapter::class],
93+
$c[LoggerInterface::class],
8594
);
8695

8796
$this[Cache::class] = static fn(self $c) => new Cache($c[Storage::class], $c['cache_ttl']);
@@ -91,6 +100,7 @@ public function __construct()
91100
$c[Storage::class],
92101
$c[ImageProcessor::class],
93102
$c[Cache::class],
103+
$c[LoggerInterface::class],
94104
);
95105
}
96106
}

src/Dto/QueryParams.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ private function __construct(
2929
int $watermarkSize,
3030
int $watermarkOpacity,
3131
) {
32-
$this->watermarkUrl = explode('://', (string) $watermarkUrl)[1] ?? null;
32+
$this->watermarkUrl = (true === str_contains((string) $watermarkUrl, '://'))
33+
? (explode('://', (string) $watermarkUrl)[1] ?? null)
34+
: $watermarkUrl
35+
;
3336

3437
if ($watermarkSize < 0) {
3538
$watermarkSize = 0;

src/Processor/ImageProcessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
use League\Flysystem\Filesystem;
1919
use League\Flysystem\FilesystemAdapter;
2020
use League\Glide\ServerFactory;
21+
use Psr\Log\LoggerInterface;
2122

2223
final class ImageProcessor
2324
{
2425
public function __construct(
2526
private readonly FilesystemAdapter $adapter,
2627
private readonly UrlFilesystemAdapter $urlFilesystemAdapter,
28+
private readonly LoggerInterface $logger,
2729
) {
2830
}
2931

@@ -40,6 +42,14 @@ public function process(string $path, QueryParams $params): string
4042
],
4143
);
4244

45+
$this->logger->info(
46+
'Process image: {path} with params {params}',
47+
[
48+
'path' => $path,
49+
'params' => json_encode($params->toArray(), flags: JSON_THROW_ON_ERROR),
50+
]
51+
);
52+
4353
return $server->makeImage(basename($path), $params->toArray());
4454
}
4555
}

0 commit comments

Comments
 (0)