1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Koded package. |
5
|
|
|
* |
6
|
|
|
* (c) Mihail Binev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Please view the LICENSE distributed with this source code |
9
|
|
|
* for the full copyright and license information. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Koded\Logging\Processors; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Abstract class Processor for creating a concrete logger implementations. |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
abstract class Processor |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int Packed integer for all log levels. If not specified, all levels |
23
|
|
|
* are included (by default) |
24
|
|
|
*/ |
25
|
|
|
protected int $levels = -1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string The log message format. |
29
|
|
|
*/ |
30
|
|
|
protected string $format = 'timestamp [levelname] message'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string Keeps all formatted log messages in this property. |
34
|
|
|
*/ |
35
|
|
|
protected string $formatted = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $settings |
39
|
|
|
*/ |
40
|
14 |
|
public function __construct(array $settings) |
41
|
|
|
{ |
42
|
14 |
|
$this->levels = (int)($settings['levels'] ?? $this->levels); |
43
|
14 |
|
$this->format = (string)($settings['format'] ?? $this->format); |
44
|
14 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Receive update from the Log instance. |
48
|
|
|
* This is where the messages are filtered and processed. |
49
|
|
|
* |
50
|
|
|
* @param array{level: int, levelname: string, message: string, timestamp: string} $message |
51
|
|
|
* The message to be processed |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
8 |
|
*/ |
55
|
|
|
public function update(array $message): void |
56
|
8 |
|
{ |
57
|
8 |
|
if (($message['level'] ?? -1) & $this->levels) { |
58
|
6 |
|
$this->process($message); |
59
|
|
|
} |
60
|
|
|
} |
61
|
8 |
|
|
62
|
|
|
/** |
63
|
|
|
* The concrete implementation of the log processor where |
64
|
|
|
* the message is filtered and delivered. |
65
|
|
|
* |
66
|
|
|
* @param array $message |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
abstract protected function process(array $message): void; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns all enabled log levels for the processor object. |
74
|
|
|
* See Logger interface constants for all available levels. |
75
|
|
|
* |
76
|
|
|
* -1 Enable all log levels |
77
|
|
|
* 0 Disable logging, the log processor should not be loaded at all |
78
|
|
|
* 1-128 Power of 2 number, for specific log levels |
79
|
|
|
* |
80
|
|
|
* @return int A packed integer for bitwise comparison |
81
|
|
|
*/ |
82
|
|
|
public function levels(): int |
83
|
4 |
|
{ |
84
|
|
|
return $this->levels; |
85
|
4 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns all messages as formatted string. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function formatted(): string |
93
|
7 |
|
{ |
94
|
|
|
return $this->formatted; |
95
|
7 |
|
} |
96
|
|
|
} |
97
|
|
|
|