1
1
<?php
2
2
namespace Enobrev ;
3
3
4
- use Exception ;
5
4
use Throwable ;
6
5
7
6
use Adbar \Dot ;
8
- use Monolog ;
7
+ use Laminas \ Diactoros \ ServerRequestFactory ;
9
8
use Monolog \Formatter \LineFormatter ;
10
9
use Monolog \Handler \SyslogHandler ;
10
+ use Monolog \Logger ;
11
11
use Psr \Http \Message \ServerRequestInterface ;
12
- use Laminas \Diactoros \ServerRequestFactory ;
13
12
14
13
class Log {
15
- /** @var Monolog\Logger */
16
- private static $ oLog ;
14
+ private static ?Logger $ oLog ;
17
15
18
- /** @var ServerRequestInterface */
19
- private static $ oServerRequest ;
16
+ private static ServerRequestInterface $ oServerRequest ;
20
17
21
- /** @var string */
22
- private static $ sService = 'Enobrev_Logger_Replace_Me ' ;
18
+ private static string $ sService = 'Enobrev_Logger_Replace_Me ' ;
23
19
24
- /** @var int */
25
- private static $ iStackLimit = 5 ;
20
+ private static int $ iStackLimit = 5 ;
26
21
27
- /** @var bool */
28
- private static $ bMetrics = false ;
22
+ private static bool $ bMetrics = false ;
29
23
30
- /** @var bool */
31
- private static $ bJSONLogs = false ;
24
+ private static bool $ bJSONLogs = false ;
32
25
33
- /** @var string */
34
- private static $ sThreadHash ;
26
+ private static string $ sThreadHash ;
35
27
36
- /** @var array */
37
- private static $ aSpans = [];
28
+ private static array $ aSpans = [];
38
29
39
30
/** @var SpanMeta[] */
40
- private static $ aSpanMetas = [];
31
+ private static array $ aSpanMetas = [];
41
32
42
- /** @var int */
43
- private static $ iGlobalIndex = 0 ;
33
+ private static int $ iGlobalIndex = 0 ;
44
34
45
- /** @var array */
46
- private static $ aDisabled = [
35
+ private static array $ aDisabled = [
47
36
'd ' => false ,
48
37
'dt ' => false
49
38
];
50
39
51
40
/**
52
- * @return Monolog\ Logger
41
+ * @return Logger
53
42
*/
54
- private static function initLogger (): \ Monolog \ Logger {
43
+ private static function initLogger (): Logger {
55
44
if (self ::$ oLog === null ) {
56
45
register_shutdown_function ([self ::class, 'summary ' ]);
57
46
58
- self ::$ oLog = new Monolog \ Logger (self ::$ sService );
47
+ self ::$ oLog = new Logger (self ::$ sService );
59
48
60
49
if (self ::$ bJSONLogs ) {
61
50
$ oFormatter = new LineFormatter ('@cee: %context% ' );
@@ -219,13 +208,13 @@ public static function disableDT(bool $bDisabled = true): void {
219
208
* @param array $aContext The log context
220
209
* @return boolean Whether the record has been processed
221
210
*/
222
- public static function d ($ sMessage , array $ aContext = array ()): bool {
211
+ public static function d (string $ sMessage , array $ aContext = array ()): bool {
223
212
if (self ::$ aDisabled ['d ' ]) {
224
213
self ::justAddContext ($ aContext );
225
214
return false ;
226
215
}
227
216
228
- return self ::addRecord (Monolog \ Logger::DEBUG , $ sMessage , $ aContext );
217
+ return self ::addRecord (Logger::DEBUG , $ sMessage , $ aContext );
229
218
}
230
219
231
220
/**
@@ -235,8 +224,8 @@ public static function d($sMessage, array $aContext = array()): bool {
235
224
* @param array $aContext The log context
236
225
* @return boolean Whether the record has been processed
237
226
*/
238
- public static function i ($ sMessage , array $ aContext = array ()): bool {
239
- return self ::addRecord (Monolog \ Logger::INFO , $ sMessage , $ aContext );
227
+ public static function i (string $ sMessage , array $ aContext = array ()): bool {
228
+ return self ::addRecord (Logger::INFO , $ sMessage , $ aContext );
240
229
}
241
230
242
231
/**
@@ -246,8 +235,8 @@ public static function i($sMessage, array $aContext = array()): bool {
246
235
* @param array $aContext The log context
247
236
* @return boolean Whether the record has been processed
248
237
*/
249
- public static function n ($ sMessage , array $ aContext = array ()): bool {
250
- return self ::addRecord (Monolog \ Logger::NOTICE , $ sMessage , $ aContext );
238
+ public static function n (string $ sMessage , array $ aContext = array ()): bool {
239
+ return self ::addRecord (Logger::NOTICE , $ sMessage , $ aContext );
251
240
}
252
241
253
242
/**
@@ -257,8 +246,8 @@ public static function n($sMessage, array $aContext = array()): bool {
257
246
* @param array $aContext The log context
258
247
* @return boolean Whether the record has been processed
259
248
*/
260
- public static function w ($ sMessage , array $ aContext = array ()): bool {
261
- return self ::addRecord (Monolog \ Logger::WARNING , $ sMessage , $ aContext );
249
+ public static function w (string $ sMessage , array $ aContext = array ()): bool {
250
+ return self ::addRecord (Logger::WARNING , $ sMessage , $ aContext );
262
251
}
263
252
264
253
/**
@@ -268,20 +257,20 @@ public static function w($sMessage, array $aContext = array()): bool {
268
257
* @param array $aContext The log context
269
258
* @return boolean Whether the record has been processed
270
259
*/
271
- public static function e ($ sMessage , array $ aContext = array ()): bool {
272
- return self ::addRecord (Monolog \ Logger::ERROR , $ sMessage , $ aContext );
260
+ public static function e (string $ sMessage , array $ aContext = array ()): bool {
261
+ return self ::addRecord (Logger::ERROR , $ sMessage , $ aContext );
273
262
}
274
263
275
264
/**
276
265
* Adds a log record at the ERROR level.
277
266
*
278
267
* @param string $sMessage The log message
279
- * @param Exception $oThrowable The exception
268
+ * @param Throwable $oThrowable The exception
280
269
* @param array $aContext The log context
281
270
*
282
271
* @return boolean Whether the record has been processed
283
272
*/
284
- public static function ex ($ sMessage , Throwable $ oThrowable , array $ aContext = array ()): bool {
273
+ public static function ex (string $ sMessage , Throwable $ oThrowable , array $ aContext = array ()): bool {
285
274
$ iTruncate = self ::$ iStackLimit ;
286
275
$ aStack = $ oThrowable ->getTrace ();
287
276
$ iStack = count ($ aStack );
@@ -315,10 +304,10 @@ public static function ex($sMessage, Throwable $oThrowable, array $aContext = ar
315
304
'context ' => self ::getContextForOutput ()
316
305
];
317
306
318
- return self ::addRecord (Monolog \ Logger::ERROR , $ sMessage , $ aContext );
307
+ return self ::addRecord (Logger::ERROR , $ sMessage , $ aContext );
319
308
}
320
309
321
- private static function replaceObjects (array $ aArgs ) {
310
+ private static function replaceObjects (array $ aArgs ): array {
322
311
$ aOutput = [];
323
312
foreach ($ aArgs as $ sKey => $ aArg ) { // Do NOT use a reference here as getTrace returns references to the actual args in the call stack which can then modify the real vars in the stack
324
313
if (is_object ($ aArg )) {
@@ -342,8 +331,8 @@ private static function replaceObjects(array $aArgs) {
342
331
* @param array $aContext The log context
343
332
* @return boolean Whether the record has been processed
344
333
*/
345
- public static function c ($ sMessage , array $ aContext = array ()): bool {
346
- return self ::addRecord (Monolog \ Logger::CRITICAL , $ sMessage , $ aContext );
334
+ public static function c (string $ sMessage , array $ aContext = array ()): bool {
335
+ return self ::addRecord (Logger::CRITICAL , $ sMessage , $ aContext );
347
336
}
348
337
349
338
/**
@@ -353,8 +342,8 @@ public static function c($sMessage, array $aContext = array()): bool {
353
342
* @param array $aContext The log context
354
343
* @return boolean Whether the record has been processed
355
344
*/
356
- public static function a ($ sMessage , array $ aContext = array ()): bool {
357
- return self ::addRecord (Monolog \ Logger::ALERT , $ sMessage , $ aContext );
345
+ public static function a (string $ sMessage , array $ aContext = array ()): bool {
346
+ return self ::addRecord (Logger::ALERT , $ sMessage , $ aContext );
358
347
}
359
348
360
349
/**
@@ -364,8 +353,8 @@ public static function a($sMessage, array $aContext = array()): bool {
364
353
* @param array $aContext The log context
365
354
* @return boolean Whether the record has been processed
366
355
*/
367
- public static function em ($ sMessage , array $ aContext = array ()): bool {
368
- return self ::addRecord (Monolog \ Logger::EMERGENCY , $ sMessage , $ aContext );
356
+ public static function em (string $ sMessage , array $ aContext = array ()): bool {
357
+ return self ::addRecord (Logger::EMERGENCY , $ sMessage , $ aContext );
369
358
}
370
359
371
360
/**
@@ -495,11 +484,9 @@ private static function getThreadHash(): string {
495
484
return self ::$ sThreadHash ;
496
485
}
497
486
498
- /** @var array */
499
- private static $ aIndices = [];
487
+ private static array $ aIndices = [];
500
488
501
- /** @var bool */
502
- private static $ bJSONParsed = false ;
489
+ private static bool $ bJSONParsed = false ;
503
490
504
491
private static function parseJSONBodyForIndices (): void {
505
492
if (self ::$ bJSONParsed ) {
@@ -647,10 +634,10 @@ public static function summary(string $sOverrideName = 'Summary'): void {
647
634
self ::getCurrentSpan ()
648
635
);
649
636
650
- self ::initLogger ()->addRecord (Monolog \ Logger::INFO , $ aMessage ['--action ' ], $ aMessage );
637
+ self ::initLogger ()->addRecord (Logger::INFO , $ aMessage ['--action ' ], $ aMessage );
651
638
}
652
639
653
- public static function getContextForOutput () {
640
+ public static function getContextForOutput (): array {
654
641
return self ::$ aSpanMetas [self ::getCurrentRequestHash ()]->Context ->all ();
655
642
}
656
643
}
0 commit comments