Passed
Push — 8.0 ( 9d1982...647794 )
by liu
02:07
created

Channel::getLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
declare(strict_types=1);
12
13
namespace think\log;
14
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\LoggerTrait;
17
use Stringable;
18
use think\contract\LogHandlerInterface;
19
use think\Event;
20
use think\event\LogRecord;
21
use think\event\LogWrite;
22
23
class Channel implements LoggerInterface
24
{
25
    use LoggerTrait;
26
27
    /**
28
     * 日志信息
29
     * @var array
30
     */
31
    protected $log = [];
32
33
    /**
34
     * 关闭日志
35
     * @var bool
36
     */
37
    protected $close = false;
38
39 9
    public function __construct(protected string $name, protected LogHandlerInterface $logger, protected array $allow, protected bool $lazy, protected Event $event)
40
    {
41 9
    }
42
43
    /**
44
     * 关闭通道
45
     */
46 3
    public function close(): void
47
    {
48 3
        $this->clear();
49 3
        $this->close = true;
50
    }
51
52
    /**
53
     * 清空日志
54
     */
55 6
    public function clear(): void
56
    {
57 6
        $this->log = [];
58
    }
59
60
    /**
61
     * 记录日志信息
62
     * @access public
63
     * @param mixed  $msg     日志信息
64
     * @param string $type    日志级别
65
     * @param array  $context 替换内容
66
     * @param bool   $lazy
67
     * @return $this
68
     */
69 6
    public function record($msg, string $type = 'info', array $context = [], bool $lazy = true)
70
    {
71 6
        if ($this->close || (!empty($this->allow) && !in_array($type, $this->allow))) {
72 3
            return $this;
73
        }
74
75 6
        if ($msg instanceof Stringable) {
76
            $msg = $msg->__toString();
77
        }
78
        
79 6
        if (is_string($msg) && !empty($context)) {
80
            $replace = [];
81
            foreach ($context as $key => $val) {
82
                $replace['{' . $key . '}'] = $val;
83
            }
84
85
            $msg = strtr($msg, $replace);
86
        }
87
88 6
        if (!empty($msg) || 0 === $msg) {
89 6
            $this->log[$type][] = $msg;
90 6
            if ($this->event) {
91 6
                $this->event->trigger(new LogRecord($type, $msg));
92
            }
93
        }
94
95 6
        if (!$this->lazy || !$lazy) {
96 3
            $this->save();
97
        }
98
99 6
        return $this;
100
    }
101
102
    /**
103
     * 实时写入日志信息
104
     * @access public
105
     * @param mixed  $msg     调试信息
106
     * @param string $type    日志级别
107
     * @param array  $context 替换内容
108
     * @return $this
109
     */
110
    public function write($msg, string $type = 'info', array $context = [])
111
    {
112
        return $this->record($msg, $type, $context, false);
113
    }
114
115
    /**
116
     * 获取日志信息
117
     * @return array
118
     */
119 3
    public function getLog(): array
120
    {
121 3
        return $this->log;
122
    }
123
124
    /**
125
     * 保存日志
126
     * @return bool
127
     */
128 6
    public function save(): bool
129
    {
130 6
        $log = $this->log;
131 6
        if ($this->event) {
132 6
            $event = new LogWrite($this->name, $log);
133 6
            $this->event->trigger($event);
134 6
            $log = $event->log;
135
        }
136
137 6
        if ($this->logger->save($log)) {
138 6
            $this->clear();
139 6
            return true;
140
        }
141
142
        return false;
143
    }
144
145
    /**
146
     * Logs with an arbitrary level.
147
     *
148
     * @param mixed  $level
149
     * @param string|Stringable  $message
150
     * @param array  $context
151
     *
152
     * @return void
153
     */
154
    public function log($level, string|Stringable $message, array $context = []): void
155
    {
156
        $this->record($message, $level, $context);
157
    }
158
159
    public function __call($method, $parameters)
160
    {
161
        $this->log($method, ...$parameters);
0 ignored issues
show
Bug introduced by
$parameters is expanded, but the parameter $message of think\log\Channel::log() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
        $this->log($method, /** @scrutinizer ignore-type */ ...$parameters);
Loading history...
162
    }
163
}
164