Skip to content

Commit ff4e79f

Browse files
committed
move logger
1 parent d91b499 commit ff4e79f

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

examples/class.php

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,7 @@
55

66
use Jcupitt\Vips;
77

8-
const LOG_FORMAT = "[%datetime%] %level_name%: %message% %context%\n";
9-
const DATE_FORMAT = "Y-m-d\TH:i:sP";
10-
11-
Vips\Image::setLogger(new class implements Psr\Log\LoggerInterface {
12-
// Use the LoggerTrait so that we only have to implement the generic
13-
// log method.
14-
use Psr\Log\LoggerTrait;
15-
16-
/**
17-
* Logs with an arbitrary level.
18-
*
19-
* @param mixed $level
20-
* @param string $message
21-
* @param array $context
22-
*
23-
* @return void
24-
*/
25-
public function log($level, $message, array $context = [])
26-
{
27-
// `Vips\Image` to string convert
28-
array_walk_recursive($context, function (&$value) {
29-
if ($value instanceof Vips\Image) {
30-
$value = (string) $value;
31-
}
32-
});
33-
34-
$strParams = [
35-
'%datetime%' => date(DATE_FORMAT),
36-
'%level_name%' => $level,
37-
'%message%' => $message,
38-
'%context%' => json_encode(
39-
$context,
40-
JSON_UNESCAPED_SLASHES |
41-
JSON_UNESCAPED_UNICODE |
42-
JSON_PRESERVE_ZERO_FRACTION
43-
),
44-
];
45-
46-
echo strtr(LOG_FORMAT, $strParams);
47-
}
48-
});
8+
public static function setLogger(LoggerInterface $logger)
499

5010
$image = Vips\Image::newFromFile($argv[1]);
5111

src/Image.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141

4242
use Psr\Log\LoggerInterface;
4343

44+
const LOG_FORMAT = "[%datetime%] %level_name%: %message% %context%\n";
45+
const DATE_FORMAT = "Y-m-d\TH:i:sP";
46+
4447
/**
4548
* This class represents a Vips image object.
4649
*
@@ -417,7 +420,51 @@ class Image extends ImageAutodoc implements \ArrayAccess
417420
*
418421
* @var LoggerInterface
419422
*/
420-
private static $logger;
423+
private static $logger;
424+
425+
/**
426+
* A basic logger, handy for debugging.
427+
*
428+
* @var LoggerInterface
429+
*/
430+
public static $debugLogger = new class implements Psr\Log\LoggerInterface {
431+
// Use the LoggerTrait so that we only have to implement the generic
432+
// log method.
433+
use Psr\Log\LoggerTrait;
434+
435+
/**
436+
* Logs with an arbitrary level.
437+
*
438+
* @param mixed $level
439+
* @param string $message
440+
* @param array $context
441+
*
442+
* @return void
443+
*/
444+
public function log($level, $message, array $context = [])
445+
{
446+
// `Vips\Image` to string convert
447+
array_walk_recursive($context, function (&$value) {
448+
if ($value instanceof Vips\Image) {
449+
$value = (string) $value;
450+
}
451+
});
452+
453+
$strParams = [
454+
'%datetime%' => date(DATE_FORMAT),
455+
'%level_name%' => $level,
456+
'%message%' => $message,
457+
'%context%' => json_encode(
458+
$context,
459+
JSON_UNESCAPED_SLASHES |
460+
JSON_UNESCAPED_UNICODE |
461+
JSON_PRESERVE_ZERO_FRACTION
462+
),
463+
];
464+
465+
echo strtr(LOG_FORMAT, $strParams);
466+
}
467+
});
421468

422469
/**
423470
* Sets a logger.

0 commit comments

Comments
 (0)