1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stu\Module\Logging; |
4
|
|
|
|
5
|
|
|
use Monolog\Handler\RotatingFileHandler; |
6
|
|
|
use Monolog\Handler\StreamHandler; |
7
|
|
|
use Monolog\Level; |
8
|
|
|
use Monolog\Logger; |
9
|
|
|
use Stu\Config\Init; |
10
|
|
|
use Stu\Module\Config\StuConfigInterface; |
11
|
|
|
|
12
|
|
|
class StuLogger |
13
|
|
|
{ |
14
|
|
|
/** @var array<string, Logger> */ |
15
|
|
|
private static array $loggers = []; |
16
|
|
|
|
17
|
|
|
private static ?Logger $loggerMock = null; |
18
|
|
|
|
19
|
4 |
|
public static function log(string $message, LogTypeEnum $type = LogTypeEnum::DEFAULT): void |
20
|
|
|
{ |
21
|
4 |
|
LogLevelEnum::INFO->log($message, self::getLogger($type)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** @param string|int|float $args */ |
25
|
|
|
public static function logf(string $information, ...$args): void |
26
|
|
|
{ |
27
|
|
|
self::log(vsprintf( |
28
|
|
|
$information, |
29
|
|
|
$args |
30
|
|
|
)); |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
public static function getLogger(LogTypeEnum $type): Logger |
34
|
|
|
{ |
35
|
6 |
|
if (self::$loggerMock !== null) { |
36
|
4 |
|
return self::$loggerMock; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
if (!array_key_exists($type->value, self::$loggers)) { |
40
|
|
|
|
41
|
1 |
|
$logPath = $type->getLogfilePath(Init::getContainer()->get(StuConfigInterface::class) |
42
|
1 |
|
->getDebugSettings()->getLoggingSettings()->getLogDirectory()); |
43
|
|
|
|
44
|
1 |
|
$logger = new Logger($type->value); |
45
|
|
|
|
46
|
1 |
|
if ($type->isRotating()) { |
47
|
1 |
|
$logger->pushHandler( |
48
|
1 |
|
new RotatingFileHandler($logPath, 10, Level::Info) |
49
|
1 |
|
); |
50
|
|
|
} else { |
51
|
1 |
|
$logger->pushHandler( |
52
|
1 |
|
new StreamHandler( |
53
|
1 |
|
$logPath |
54
|
1 |
|
), |
55
|
1 |
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
self::$loggers[$type->value] = $logger; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return self::$loggers[$type->value]; |
62
|
|
|
} |
63
|
|
|
|
64
|
13 |
|
public static function setMock(?Logger $mock): void |
65
|
|
|
{ |
66
|
13 |
|
self::$loggerMock = $mock; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|