ForrestLogger   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 59
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A info() 0 7 3
A warn() 0 7 3
A setLogLevel() 0 6 2
A addLogger() 0 3 1
A error() 0 7 3
1
<?php
2
3
namespace Startwind\Forrest\Logger;
4
5
class ForrestLogger
6
{
7
    public const LEVEL_ERROR = 100;
8
    public const LEVEL_WARN = 200;
9
    public const LEVEL_INFO = 300;
10
11
    /**
12
     * @var Logger[]
13
     */
14
    private static array $logger = [];
15
16
    private static int $logLevel = self::LEVEL_ERROR;
17
18
    private static array $levels = [
19
        self::LEVEL_ERROR,
20
        self::LEVEL_WARN,
21
        self::LEVEL_INFO
22
    ];
23
24
    public static function setLogLevel(int $logLevel): void
25
    {
26
        if (!in_array($logLevel, self::$levels)) {
27
            throw new \RuntimeException('The given log level (' . $logLevel . ') does not exists.');
28
        }
29
        self::$logLevel = $logLevel;
30
    }
31
32
    public static function addLogger($key, Logger $logger): void
33
    {
34
        self::$logger[$key] = $logger;
35
    }
36
37
    public static function info($message): void
38
    {
39
        if (self::$logLevel < self::LEVEL_INFO) {
40
            return;
41
        }
42
        foreach (self::$logger as $logger) {
43
            $logger->info($message);
44
        }
45
    }
46
47
    public static function warn($message): void
48
    {
49
        if (self::$logLevel < self::LEVEL_WARN) {
50
            return;
51
        }
52
        foreach (self::$logger as $logger) {
53
            $logger->warn($message);
54
        }
55
    }
56
57
    public static function error($message): void
58
    {
59
        if (self::$logLevel < self::LEVEL_ERROR) {
60
            return;
61
        }
62
        foreach (self::$logger as $logger) {
63
            $logger->error($message);
64
        }
65
    }
66
}
67