Passed
Push — dev ( 896d5d...6a73c6 )
by Janko
07:03
created

StuLogger::getLogger()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
nc 3
nop 1
dl 0
loc 25
c 1
b 0
f 1
cc 3
ccs 16
cts 16
cp 1
crap 3
rs 9.8333
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 4
    public static function log(string $message, LogTypeEnum $type = LogTypeEnum::DEFAULT): void
18
    {
19 4
        LogLevelEnum::INFO->log($message, self::getLogger($type));
20
    }
21
22
    /** @param string|int|float $args */
23
    public static function logf(string $information, ...$args): void
24
    {
25
        self::log(vsprintf(
26
            $information,
27
            $args
28
        ));
29
    }
30
31 6
    public static function getLogger(LogTypeEnum $type): Logger
32
    {
33 6
        if (!array_key_exists($type->value, self::$loggers)) {
34
35 2
            $logPath = $type->getLogfilePath(Init::getContainer()->get(StuConfigInterface::class)
36 2
                ->getDebugSettings()->getLoggingSettings()->getLogDirectory());
37
38 2
            $logger = new Logger($type->value);
39
40 2
            if ($type->isRotating()) {
41 2
                $logger->pushHandler(
42 2
                    new RotatingFileHandler($logPath, 10, Level::Info)
43 2
                );
44
            } else {
45 1
                $logger->pushHandler(
46 1
                    new StreamHandler(
47 1
                        $logPath
48 1
                    ),
49 1
                );
50
            }
51
52 2
            self::$loggers[$type->value] = $logger;
53
        }
54
55 6
        return self::$loggers[$type->value];
56
    }
57
}
58