1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/telemetry package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Telemetry\TelemetryClient; |
13
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Psr\Log\LoggerTrait; |
16
|
|
|
use Slick\Telemetry\TelemetryClient; |
17
|
|
|
use Slick\Telemetry\Trackable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PsrLoggerClient |
21
|
|
|
* |
22
|
|
|
* @package Slick\Telemetry\TelemetryClient |
23
|
|
|
*/ |
24
|
|
|
final class PsrLoggerClient implements TelemetryClient |
25
|
|
|
{ |
26
|
|
|
use LoggerTrait; |
27
|
|
|
use TelemetryClientTrait; |
28
|
|
|
use RequestFactoryTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creates a PsrLoggerClient |
32
|
|
|
* |
33
|
|
|
* @param LoggerInterface $logger |
34
|
|
|
*/ |
35
|
|
|
public function __construct(LoggerInterface $logger) |
36
|
|
|
{ |
37
|
|
|
$this->logger = $logger; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
*/ |
43
|
|
|
public function log($level, $message, array $context = array()) |
44
|
|
|
{ |
45
|
|
|
list($level, $context) = $this->overrideLevel($level, $context); |
46
|
|
|
$this->logger->log($level, $message, $context); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
public function track(Trackable $trackableData): TelemetryClient |
53
|
|
|
{ |
54
|
|
|
$context = array_merge((array) $this->context, (array) $trackableData->context()); |
55
|
|
|
$message = $this->interpolate($trackableData->message(), $context); |
56
|
|
|
$this->log($trackableData->logLevel(), $message, $context); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Interpolates context values into the message placeholders. |
62
|
|
|
* |
63
|
|
|
* @param string $message |
64
|
|
|
* @param array $context |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
private function interpolate(string $message, iterable $context = []): string |
68
|
|
|
{ |
69
|
|
|
// build a replacement array with braces around the context keys |
70
|
|
|
$replace = array(); |
71
|
|
|
foreach ($context as $key => $val) { |
72
|
|
|
// check that the value can be cast to string |
73
|
|
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
74
|
|
|
$replace['{' . $key . '}'] = $val; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// interpolate replacement values into the message and return |
79
|
|
|
return strtr($message, $replace); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function overrideLevel($level, array $context): array |
83
|
|
|
{ |
84
|
|
|
if (array_key_exists('level', $context)) { |
85
|
|
|
$level = $context['level']; |
86
|
|
|
unset($context['level']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return [$level, $context]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|