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

StuLogger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 78.26%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
dl 0
loc 44
c 1
b 0
f 1
ccs 18
cts 23
cp 0.7826
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogger() 0 25 3
A log() 0 3 1
A logf() 0 5 1
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