Passed
Push — master ( 093846...a50b30 )
by 世昌
05:11 queued 10s
created

ConsoleLogger::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 3
dl 0
loc 4
rs 10
c 1
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