|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Derafu: Biblioteca PHP (Núcleo). |
|
7
|
|
|
* Copyright (C) Derafu <https://www.derafu.org> |
|
8
|
|
|
* |
|
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
|
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
|
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
|
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
|
13
|
|
|
* |
|
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
|
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
|
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
|
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
|
18
|
|
|
* |
|
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de GNU |
|
20
|
|
|
* junto a este programa. |
|
21
|
|
|
* |
|
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Derafu\Lib\Core\Package\Prime\Component\Log\Entity; |
|
26
|
|
|
|
|
27
|
|
|
use InvalidArgumentException; |
|
28
|
|
|
use LogicException; |
|
29
|
|
|
use Monolog\Level as MonologLevel; |
|
30
|
|
|
use Psr\Log\LogLevel as PsrLogLevel; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Clase para los niveles de registros de la bitácora. |
|
34
|
|
|
*/ |
|
35
|
|
|
class Level |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Información de depuración detallada. |
|
39
|
|
|
* |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
public const DEBUG = MonologLevel::Debug->value; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Eventos de interés. |
|
46
|
|
|
* |
|
47
|
|
|
* Ejemplos: |
|
48
|
|
|
* |
|
49
|
|
|
* - Registro de inicios de sesión de usuario. |
|
50
|
|
|
* - Registro de consultas SQL. |
|
51
|
|
|
* |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
public const INFO = MonologLevel::Info->value; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Registros de eventos poco comúnes. |
|
58
|
|
|
* |
|
59
|
|
|
* @var int |
|
60
|
|
|
*/ |
|
61
|
|
|
public const NOTICE = MonologLevel::Notice->value; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Eventos excepcionales que no son errores. |
|
65
|
|
|
* |
|
66
|
|
|
* Ejemplos: |
|
67
|
|
|
* |
|
68
|
|
|
* - Usos de APIs obsoletas. |
|
69
|
|
|
* - Usos desaconsejados ("pobre") de APIs. |
|
70
|
|
|
* - Usos no deseables o llamadas "poco correctas" que no necesariamente |
|
71
|
|
|
* están mal pero podrían ser hechas de mejor forma. |
|
72
|
|
|
* |
|
73
|
|
|
* @var int |
|
74
|
|
|
*/ |
|
75
|
|
|
public const WARNING = MonologLevel::Warning->value; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Errores de tiempo de ejecución. |
|
79
|
|
|
*/ |
|
80
|
|
|
public const ERROR = MonologLevel::Error->value; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Condiciones críticas. |
|
84
|
|
|
*/ |
|
85
|
|
|
public const CRITICAL = MonologLevel::Critical->value; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Alertas que requieren que una acción sea tomada de manera inmediata. |
|
89
|
|
|
* |
|
90
|
|
|
* Ejemplos: |
|
91
|
|
|
* |
|
92
|
|
|
* - Aplicación caída. |
|
93
|
|
|
* - Tests que no pasan. |
|
94
|
|
|
* - Fuentes de datos no disponibles. |
|
95
|
|
|
* |
|
96
|
|
|
* Esto debe lanzar una alerta que si o si debe llegar a "alguien" para que |
|
97
|
|
|
* la revise ASAP (as soon as possible). |
|
98
|
|
|
*/ |
|
99
|
|
|
public const ALERT = MonologLevel::Alert->value; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Alertas urgentes. |
|
103
|
|
|
*/ |
|
104
|
|
|
public const EMERGENCY = MonologLevel::Emergency->value; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Mapeo de niveles de logs de diferentes sistemas o fuentes a los niveles |
|
108
|
|
|
* usados por el servicio de bitácoras de la biblioteca. |
|
109
|
|
|
* |
|
110
|
|
|
* @var array<int|string,int> |
|
111
|
|
|
*/ |
|
112
|
|
|
private const LEVELS = [ |
|
113
|
|
|
// Logs de PHP / RFC5424 (?). |
|
114
|
|
|
LOG_DEBUG => self::DEBUG, |
|
115
|
|
|
LOG_INFO => self::INFO, |
|
116
|
|
|
LOG_NOTICE => self::NOTICE, |
|
117
|
|
|
LOG_WARNING => self::WARNING, |
|
118
|
|
|
LOG_ERR => self::ERROR, |
|
119
|
|
|
LOG_CRIT => self::CRITICAL, |
|
120
|
|
|
LOG_ALERT => self::ALERT, |
|
121
|
|
|
LOG_EMERG => self::EMERGENCY, |
|
122
|
|
|
|
|
123
|
|
|
// Logs de PSR-3. |
|
124
|
|
|
PsrLogLevel::DEBUG => self::DEBUG, |
|
125
|
|
|
PsrLogLevel::INFO => self::INFO, |
|
126
|
|
|
PsrLogLevel::NOTICE => self::NOTICE, |
|
127
|
|
|
PsrLogLevel::WARNING => self::WARNING, |
|
128
|
|
|
PsrLogLevel::ERROR => self::ERROR, |
|
129
|
|
|
PsrLogLevel::CRITICAL => self::CRITICAL, |
|
130
|
|
|
PsrLogLevel::ALERT => self::ALERT, |
|
131
|
|
|
PsrLogLevel::EMERGENCY => self::EMERGENCY, |
|
132
|
|
|
]; |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Código del nivel. |
|
136
|
|
|
* |
|
137
|
|
|
* @var integer |
|
138
|
|
|
*/ |
|
139
|
|
|
private int $code; |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Constructor de la entidad. |
|
143
|
|
|
* |
|
144
|
|
|
* @param integer|string $code |
|
145
|
|
|
*/ |
|
146
|
29 |
|
public function __construct(int|string $code) |
|
147
|
|
|
{ |
|
148
|
29 |
|
$this->setCode($code); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Asigna el código de nivel del registro de la bitácora. |
|
153
|
|
|
* |
|
154
|
|
|
* @param int|string $code Código de nivel en cualquier formato soportado. |
|
155
|
|
|
* @return static |
|
156
|
|
|
* @throws LogicException Cuando el nivel es un string no soportado. |
|
157
|
|
|
*/ |
|
158
|
29 |
|
private function setCode(int|string $code): static |
|
159
|
|
|
{ |
|
160
|
29 |
|
if (isset(self::LEVELS[$code])) { |
|
161
|
16 |
|
$this->code = self::LEVELS[$code]; |
|
162
|
|
|
|
|
163
|
16 |
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
29 |
|
if (is_string($code)) { |
|
167
|
|
|
throw new LogicException(sprintf( |
|
168
|
|
|
'El nivel de registro de bitácora (logging) %s no está soportado.', |
|
169
|
|
|
$code |
|
170
|
|
|
)); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
29 |
|
$this->code = $code; |
|
174
|
|
|
|
|
175
|
29 |
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Entrega el código de nivel. |
|
180
|
|
|
* |
|
181
|
|
|
* El código se entrega normalizado. |
|
182
|
|
|
* |
|
183
|
|
|
* @return integer |
|
184
|
|
|
*/ |
|
185
|
29 |
|
public function getCode(): int |
|
186
|
|
|
{ |
|
187
|
29 |
|
return $this->code; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Obtiene el nombre del nivel. |
|
192
|
|
|
* |
|
193
|
|
|
* @return string Nombre del nivel. |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getName(): string |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->getMonologLevel()->name; |
|
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Obtiene la instancia "enum" del nivel. |
|
202
|
|
|
* |
|
203
|
|
|
* @return MonologLevel |
|
204
|
|
|
*/ |
|
205
|
24 |
|
public function getMonologLevel(): MonologLevel |
|
206
|
|
|
{ |
|
207
|
24 |
|
$level = MonologLevel::tryFrom($this->code); |
|
208
|
|
|
|
|
209
|
24 |
|
if ($level === null) { |
|
210
|
|
|
throw new InvalidArgumentException(sprintf( |
|
211
|
|
|
'El código de nivel de log %d es inválido como nivel de Monolog.', |
|
212
|
|
|
$this->code |
|
213
|
|
|
)); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
24 |
|
return $level; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|