Test Failed
Push — dev ( e79643...3785bc )
by 世昌
04:25
created

ConsoleLogger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace suda\framework\debug\log\logger;
4
5
6
use Psr\Log\LogLevel;
7
use Psr\Log\AbstractLogger;
8
use suda\framework\debug\Debug;
9
10
/**
11
 * 控制台日志输出
12
 */
13
class ConsoleLogger extends AbstractLogger
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $level;
19
20
    public function __construct(string $level)
21
    {
22
        $this->level = $level;
23
    }
24
25
    /**
26
     * @param mixed $level
27
     * @param string $message
28
     * @param array $context
29
     */
30
    public function log($level, $message, array $context = [])
31
    {
32
        if (Debug::compare($level, $this->level) >= 0) {
33
            print date('Y-m-d H:i:s') . ' ' . $this->interpolate($message, $context) . PHP_EOL;
34
        }
35
    }
36
37
    /**
38
     * @param string $message
39
     * @param array $context
40
     * @return string
41
     */
42
    public function interpolate(string $message, array $context)
43
    {
44
        $replace = [];
45
        foreach ($context as $key => $val) {
46
            $replace['{' . $key . '}'] = $val;
47
        }
48
        return strtr($message, $replace);
49
    }
50
}
51