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\Shared\Base\ObjectAbstract; |
20
|
|
|
use Phossa2\Logger\Traits\ExtendedLoggerTrait; |
21
|
|
|
use Phossa2\Logger\Entry\LogEntryPrototypeTrait; |
22
|
|
|
use Phossa2\Logger\Entry\LogEntryPrototypeInterface; |
23
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Logger |
27
|
|
|
* |
28
|
|
|
* Implementation of LoggerInterface |
29
|
|
|
* |
30
|
|
|
* @package Phossa2\Logger |
31
|
|
|
* @author Hong Zhang <[email protected]> |
32
|
|
|
* @see ObjectAbstract |
33
|
|
|
* @see LoggerInterface |
34
|
|
|
* @see LogEntryPrototypeInterface |
35
|
|
|
* @version 2.0.0 |
36
|
|
|
* @since 2.0.0 added |
37
|
|
|
*/ |
38
|
|
|
class Logger extends ObjectAbstract implements LoggerInterface, LogEntryPrototypeInterface |
39
|
|
|
{ |
40
|
|
|
use LoggerTrait, LogEntryPrototypeTrait, ExtendedLoggerTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Instantiate with default channel name and log entry prototype |
44
|
|
|
* |
45
|
|
|
* @param string $channel default channel |
46
|
|
|
* @param LogEntryInterface $entryPrototype if any |
47
|
|
|
* @access protected |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
/*# string */ $channel = 'LOGGER', |
51
|
|
|
LogEntryInterface $entryPrototype = null |
52
|
|
|
) { |
53
|
|
|
$this->default_channel = strtoupper($channel); |
54
|
|
|
$this->setLogEntryPrototype($entryPrototype); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set/With current channel, followed by any log() method |
59
|
|
|
* |
60
|
|
|
* @param string $channel current channel |
61
|
|
|
* @return $this |
62
|
|
|
* @access protected |
63
|
|
|
*/ |
64
|
|
|
public function with(/*# string */ $channel) |
65
|
|
|
{ |
66
|
|
|
$this->current_channel = strtoupper($channel); |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Logs with an arbitrary level. |
72
|
|
|
* |
73
|
|
|
* @param mixed $level |
74
|
|
|
* @param string $message |
75
|
|
|
* @param array $context |
76
|
|
|
* @return null |
77
|
|
|
*/ |
78
|
|
|
public function log($level, $message, array $context = array()) |
79
|
|
|
{ |
80
|
|
|
// create log entry |
81
|
|
|
$entry = $this->newLogEntry( |
82
|
|
|
$this->getChannel(), $level, $message, $context |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
// run processors |
86
|
|
|
$this->runProcessors($entry); |
87
|
|
|
|
88
|
|
|
// run handlers |
89
|
|
|
$this->runHandlers($entry); |
90
|
|
|
|
91
|
|
|
// unset current channel |
92
|
|
|
$this->current_channel = null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add handler to the channel with priority |
97
|
|
|
* |
98
|
|
|
* @param callable $handler |
99
|
|
|
* @param string $channel channel to listen to |
100
|
|
|
* @param int $priority |
101
|
|
|
* @return $this |
102
|
|
|
* @access public |
103
|
|
|
* @api |
104
|
|
|
*/ |
105
|
|
|
public function addHandler( |
106
|
|
|
callable $handler, |
107
|
|
|
/*# string */ $channel = '*', |
108
|
|
|
/*# int */ $priority = 0 |
109
|
|
|
) { |
110
|
|
|
return $this->addCallable('handlers', $handler, $channel, $priority); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Remove this handler from the channel |
115
|
|
|
* |
116
|
|
|
* if $channel == '', then remove this handler from all channels |
117
|
|
|
* |
118
|
|
|
* @param callable|string $handlerOrClassname |
119
|
|
|
* @param string $channel |
120
|
|
|
* @return $this |
121
|
|
|
* @access public |
122
|
|
|
* @api |
123
|
|
|
*/ |
124
|
|
|
public function removeHandler($handlerOrClassname, $channel = '') |
125
|
|
|
{ |
126
|
|
|
return $this->removeCallable('handlers', $handlerOrClassname, $channel); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add processor to the channel with priority |
131
|
|
|
* |
132
|
|
|
* @param callable $processor |
133
|
|
|
* @param string $channel channel to listen to |
134
|
|
|
* @param int $priority |
135
|
|
|
* @return $this |
136
|
|
|
* @access public |
137
|
|
|
* @api |
138
|
|
|
*/ |
139
|
|
|
public function addProcessor( |
140
|
|
|
callable $processor, |
141
|
|
|
/*# string */ $channel = '*', |
142
|
|
|
/*# int */ $priority = 0 |
143
|
|
|
) { |
144
|
|
|
return $this->addCallable('processors', $processor, $channel, $priority); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Remove this $processor from $channel |
149
|
|
|
* |
150
|
|
|
* if $channel == '', then remove processor from all channels |
151
|
|
|
* |
152
|
|
|
* @param callable|string $processorOrClassname |
153
|
|
|
* @param string $channel |
154
|
|
|
* @return $this |
155
|
|
|
* @access public |
156
|
|
|
* @api |
157
|
|
|
*/ |
158
|
|
|
public function removeProcessor($processorOrClassname, $channel = '') |
159
|
|
|
{ |
160
|
|
|
return $this->removeCallable( |
161
|
|
|
'processors', $processorOrClassname, $channel |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|