|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Phoole (PHP7.2+) |
|
5
|
|
|
* |
|
6
|
|
|
* @category Library |
|
7
|
|
|
* @package Phoole\Logger |
|
8
|
|
|
* @copyright Copyright (c) 2019 Hong Zhang |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phoole\Logger; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerTrait; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
use Phoole\Logger\Entry\LogEntry; |
|
17
|
|
|
use Phoole\Logger\Entry\LogEntryInterface; |
|
18
|
|
|
use Phoole\Logger\Handler\HandlerAwareTrait; |
|
19
|
|
|
use Phoole\Logger\Handler\HandlerAwareInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Logger |
|
23
|
|
|
* |
|
24
|
|
|
* @package Phoole\Logger |
|
25
|
|
|
*/ |
|
26
|
|
|
class Logger implements LoggerInterface, HandlerAwareInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use LoggerTrait; |
|
29
|
|
|
use HandlerAwareTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $channel; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Channel usually is APP ID |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $channel |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(string $channel) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->channel = $channel; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritDoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function log($level, $message, array $context = array()) |
|
50
|
|
|
{ |
|
51
|
|
|
$entry = ($this->initEntry($message, $level, $context))->process(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->getHandlers($entry) as $handler) { |
|
54
|
|
|
$handler($entry); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param LogEntryInterface|string $message |
|
60
|
|
|
* @param string $level |
|
61
|
|
|
* @param array $context |
|
62
|
|
|
* @return LogEntryInterface |
|
63
|
|
|
* @throws \InvalidArgumentException if message not right |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function initEntry($message, string $level, array $context): LogEntryInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$entry = $this->validate($message); |
|
68
|
|
|
|
|
69
|
|
|
// update channel name in context |
|
70
|
|
|
$this->setChannel($context); |
|
71
|
|
|
|
|
72
|
|
|
return $entry |
|
73
|
|
|
->setLevel($level) |
|
74
|
|
|
->setContext(array_merge($entry->getContext(), $context)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $message |
|
79
|
|
|
* @return LogEntryInterface |
|
80
|
|
|
* @throws \InvalidArgumentException if message not right |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function validate($message): LogEntryInterface |
|
83
|
|
|
{ |
|
84
|
|
|
if (is_string($message)) { |
|
85
|
|
|
$entry = new LogEntry($message); |
|
86
|
|
|
} elseif (is_object($message) && $message instanceof LogEntryInterface) { |
|
87
|
|
|
$entry = $message; |
|
88
|
|
|
} else { |
|
89
|
|
|
throw new \InvalidArgumentException("not valid message " . (string) $message); |
|
90
|
|
|
} |
|
91
|
|
|
return $entry; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set channel name in context |
|
96
|
|
|
* |
|
97
|
|
|
* @param array &$context |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function setChannel(array &$context) |
|
100
|
|
|
{ |
|
101
|
|
|
if (!isset($context['__channel'])) { |
|
102
|
|
|
$context['__channel'] = $this->channel; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |