1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Logger |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Logger; |
16
|
|
|
|
17
|
|
|
use Psr\Log\LoggerTrait; |
18
|
|
|
use Psr\Log\LoggerInterface; |
19
|
|
|
use Phossa2\Logger\Message\Message; |
20
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
21
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
22
|
|
|
use Phossa2\Logger\Traits\ExtendedLoggerTrait; |
23
|
|
|
use Phossa2\Logger\Entry\LogEntryPrototypeTrait; |
24
|
|
|
use Phossa2\Logger\Entry\LogEntryPrototypeInterface; |
25
|
|
|
use Phossa2\Logger\Exception\InvalidArgumentException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Logger |
29
|
|
|
* |
30
|
|
|
* Implementation of LoggerInterface |
31
|
|
|
* |
32
|
|
|
* @package Phossa2\Logger |
33
|
|
|
* @author Hong Zhang <[email protected]> |
34
|
|
|
* @see ObjectAbstract |
35
|
|
|
* @see LoggerInterface |
36
|
|
|
* @see LogEntryPrototypeInterface |
37
|
|
|
* @version 2.0.0 |
38
|
|
|
* @since 2.0.0 added |
39
|
|
|
* @since 2.0.1 updated addHandler() with level support |
40
|
|
|
*/ |
41
|
|
|
class Logger extends ObjectAbstract implements LoggerInterface, LogEntryPrototypeInterface |
42
|
|
|
{ |
43
|
|
|
use LoggerTrait, LogEntryPrototypeTrait, ExtendedLoggerTrait; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Instantiate with default channel name and log entry prototype |
47
|
|
|
* |
48
|
|
|
* @param string $channel default channel |
49
|
|
|
* @param LogEntryInterface $entryPrototype if any |
50
|
|
|
* @access protected |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
/*# string */ $channel = 'LOGGER', |
54
|
|
|
LogEntryInterface $entryPrototype = null |
55
|
|
|
) { |
56
|
|
|
$this->default_channel = strtoupper($channel); |
57
|
|
|
$this->setLogEntryPrototype($entryPrototype); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set/With current channel, followed by any log() method |
62
|
|
|
* |
63
|
|
|
* @param string $channel current channel |
64
|
|
|
* @return $this |
65
|
|
|
* @access protected |
66
|
|
|
*/ |
67
|
|
|
public function with(/*# string */ $channel) |
68
|
|
|
{ |
69
|
|
|
$this->current_channel = strtoupper($channel); |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Logs with an arbitrary level. |
75
|
|
|
* |
76
|
|
|
* @param mixed $level |
77
|
|
|
* @param string $message |
78
|
|
|
* @param array $context |
79
|
|
|
* @return null |
80
|
|
|
*/ |
81
|
|
|
public function log($level, $message, array $context = array()) |
82
|
|
|
{ |
83
|
|
|
// create log entry |
84
|
|
|
$entry = $this->newLogEntry( |
85
|
|
|
$this->getChannel(), |
86
|
|
|
$level, |
87
|
|
|
$message, |
88
|
|
|
$context |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
// run processors |
92
|
|
|
$this->runProcessors($entry); |
93
|
|
|
|
94
|
|
|
// run handlers |
95
|
|
|
$this->runHandlers($entry); |
96
|
|
|
|
97
|
|
|
// unset current channel |
98
|
|
|
$this->current_channel = null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add handler to the channel with priority |
103
|
|
|
* |
104
|
|
|
* @param string $level the level this handler is handling |
105
|
|
|
* @param callable $handler |
106
|
|
|
* @param string $channel channel to listen to |
107
|
|
|
* @param int $priority |
108
|
|
|
* @return $this |
109
|
|
|
* @access public |
110
|
|
|
* @since 2.0.1 added level param |
111
|
|
|
* @api |
112
|
|
|
*/ |
113
|
|
|
public function addHandler( |
114
|
|
|
/*# string */ $level, |
115
|
|
|
callable $handler, |
116
|
|
|
/*# string */ $channel = '*', |
117
|
|
|
/*# int */ $priority = 0 |
118
|
|
|
) { |
119
|
|
|
// check level |
120
|
|
View Code Duplication |
if (!isset(LogLevel::$levels[$level])) { |
121
|
|
|
throw new InvalidArgumentException( |
122
|
|
|
Message::get(Message::LOG_LEVEL_INVALID, $level), |
123
|
|
|
Message::LOG_LEVEL_INVALID |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->addCallable( |
128
|
|
|
'handlers', |
129
|
|
|
$handler, |
130
|
|
|
$channel, |
131
|
|
|
$priority, |
132
|
|
|
$level |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Remove this handler from the channel |
138
|
|
|
* |
139
|
|
|
* if $channel == '', then remove this handler from all channels |
140
|
|
|
* |
141
|
|
|
* @param callable|string $handlerOrClassname |
142
|
|
|
* @param string $channel |
143
|
|
|
* @return $this |
144
|
|
|
* @access public |
145
|
|
|
* @api |
146
|
|
|
*/ |
147
|
|
|
public function removeHandler($handlerOrClassname, $channel = '') |
148
|
|
|
{ |
149
|
|
|
return $this->removeCallable('handlers', $handlerOrClassname, $channel); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add processor to the channel with priority |
154
|
|
|
* |
155
|
|
|
* @param callable $processor |
156
|
|
|
* @param string $channel channel to listen to |
157
|
|
|
* @param int $priority |
158
|
|
|
* @return $this |
159
|
|
|
* @access public |
160
|
|
|
* @api |
161
|
|
|
*/ |
162
|
|
|
public function addProcessor( |
163
|
|
|
callable $processor, |
164
|
|
|
/*# string */ $channel = '*', |
165
|
|
|
/*# int */ $priority = 0 |
166
|
|
|
) { |
167
|
|
|
return $this->addCallable( |
168
|
|
|
'processors', |
169
|
|
|
$processor, |
170
|
|
|
$channel, |
171
|
|
|
$priority |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Remove this $processor from $channel |
177
|
|
|
* |
178
|
|
|
* if $channel == '', then remove processor from all channels |
179
|
|
|
* |
180
|
|
|
* @param callable|string $processorOrClassname |
181
|
|
|
* @param string $channel |
182
|
|
|
* @return $this |
183
|
|
|
* @access public |
184
|
|
|
* @api |
185
|
|
|
*/ |
186
|
|
|
public function removeProcessor($processorOrClassname, $channel = '') |
187
|
|
|
{ |
188
|
|
|
return $this->removeCallable( |
189
|
|
|
'processors', |
190
|
|
|
$processorOrClassname, |
191
|
|
|
$channel |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|