-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.php
151 lines (139 loc) · 4.55 KB
/
Log.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php declare(strict_types=1);
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <[email protected]>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*
*/
namespace Koded\Logging;
use Koded\Logging\Processors\{Cli, Processor};
use DateTimeZone;
use Psr\Log\LoggerTrait;
use Stringable;
use Throwable;
use function constant;
use function date_create_immutable;
use function spl_object_id;
use function strtoupper;
use function strtr;
use function timezone_open;
/**
* Class Log for logging different types of application messages.
*
* $log->notice('foo');
* $log->warn('bar');
*
* CONFIGURATION PARAMETERS (Log class)
*
* - processors (array)
* An array of log processors. Every processor is defined in array with it's own
* configuration parameters, but ALL must have the following:
*
* - class (string) [required]
* The name of the log processor class.
* Can create multiple same instances with different config
* parameters.
*
* - levels (integer) [optional], default: -1 (for all levels)
* Packed integer for bitwise comparison. See the constants in this
* class.
*
* Example: Log::INFO | Log::ERROR | Log::ALERT
* Processor with these log levels will store only
* info, error and warning type messages.
*
* - dateformat (string) [optional], default: d/m/Y H:i:s.u
* The date format for the log message.
*
* - timezone (string) [optional], default: UTC
* The desired timezone for the DateTimeZone object.
*
*
* CONFIGURATION PARAMETERS (Processor class)
* Every processor may have its own specific parameters.
*
*/
class Log implements Logger
{
use LoggerTrait;
private const DATE_FORMAT = 'd/m/Y H:i:s.u';
private DateTimeZone $timezone;
/**
* Creates all requested log processors.
*
* @param array<int, Processor> $processors a list of log processors
* @param string $dateformat The date format for the messages
* @param string $timezone The timezone for the messages
*/
public function __construct(
private array $processors = [],
private string $dateformat = self::DATE_FORMAT,
string $timezone = 'UTC')
{
$this->timezone = @timezone_open($timezone) ?: timezone_open('UTC');
foreach ($processors as $i => $processor) {
unset($this->processors[$i]);
$this->attach(new $processor['class']($processor));
}
}
public function log($level, string|Stringable $message, array $context = []): void
{
try {
$levelName = strtoupper($level);
$level = constant('static::' . $levelName);
} catch (Throwable) {
$levelName = 'LOG';
$level = -1;
}
foreach ($this->processors as $processor) {
$processor->update([
'level' => $level,
'levelname' => $levelName,
'message' => $this->formatMessage($message, $context),
'timestamp' => $this->now(),
]);
}
}
public function attach(Processor $processor): Logger
{
if (0 !== $processor->levels()) {
$this->processors[spl_object_id($processor)] = $processor;
}
return $this;
}
public function detach(Processor $processor): Logger
{
unset($this->processors[spl_object_id($processor)]);
return $this;
}
public function exception(Throwable $e, Processor $processor = null): void
{
$this->attach($processor ??= new Cli([]));
$this->critical($e->getMessage() . PHP_EOL . ' [Trace]: ' . $e->getTraceAsString());
$this->detach($processor);
}
/**
* Parses the message as in the interface specification.
*
* @param object|string $message A string or object that implements __toString
* @param array $params [optional] Arbitrary data with key-value pairs replacements
*
* @return string
*/
private function formatMessage(object|string $message, array $params = []): string
{
$replacements = [];
foreach ($params as $k => $v) {
$replacements['{' . $k . '}'] = $v;
}
return strtr((string)$message, $replacements);
}
private function now(): string
{
return date_create_immutable('now', $this->timezone)
->format($this->dateformat);
}
}