1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Profiler; |
6
|
|
|
|
7
|
|
|
use Psr\Log\InvalidArgumentException; |
8
|
|
|
use Psr\Log\LoggerTrait; |
9
|
|
|
use Psr\Log\LogLevel; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Message is a data object that stores profile message data. |
13
|
|
|
*/ |
14
|
|
|
final class Message |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $level Message level. |
18
|
|
|
* @param string $token Message token. |
19
|
|
|
* @param array $context Message context with a following keys: |
20
|
|
|
* |
21
|
|
|
* - category: string, message category. |
22
|
|
|
* - memory: int, memory usage in bytes, obtained by `memory_get_usage()`. |
23
|
|
|
* - time: float, message timestamp obtained by microtime(true). |
24
|
|
|
* - trace: array, debug backtrace, contains the application code call stacks. |
25
|
|
|
* |
26
|
|
|
* @throws InvalidArgumentException for invalid log message level. |
27
|
|
|
* |
28
|
|
|
* @see LoggerTrait::log() |
29
|
|
|
* @see LogLevel |
30
|
|
|
*/ |
31
|
15 |
|
public function __construct(private string $level, private string $token, private array $context = []) |
32
|
|
|
{ |
33
|
15 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Gets a message level. |
37
|
|
|
* |
38
|
|
|
* @return string Log message level. |
39
|
|
|
*/ |
40
|
6 |
|
public function level(): string |
41
|
|
|
{ |
42
|
6 |
|
return $this->level; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets a message token. |
47
|
|
|
* |
48
|
|
|
* @return string Message token. |
49
|
|
|
*/ |
50
|
9 |
|
public function token(): string |
51
|
|
|
{ |
52
|
9 |
|
return $this->token; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns a value of the context parameter for the specified name. |
57
|
|
|
* |
58
|
|
|
* If no name is specified, the entire context is returned. |
59
|
|
|
* |
60
|
|
|
* @param string|null $name The context parameter name. |
61
|
|
|
* @param mixed $default If the context parameter does not exist, the `$default` will be returned. |
62
|
|
|
* |
63
|
|
|
* @psalm-return ($name is null ? array : mixed|null) |
64
|
|
|
*/ |
65
|
9 |
|
public function context(string $name = null, mixed $default = null): mixed |
66
|
|
|
{ |
67
|
9 |
|
if ($name === null) { |
68
|
8 |
|
return $this->context; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var array<array-key, mixed> */ |
72
|
4 |
|
return $this->context[$name] ?? $default; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|