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
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Logger |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Logger |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @see ObjectAbstract |
30
|
|
|
* @see LoggerInterface |
31
|
|
|
* @version 2.0.0 |
32
|
|
|
* @since 2.0.0 added |
33
|
|
|
*/ |
34
|
|
|
class Logger extends ObjectAbstract implements LoggerInterface, LogEntryPrototypeInterface |
35
|
|
|
{ |
36
|
|
|
use LoggerTrait, LogEntryPrototypeTrait, ExtendedLoggerTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Must set current channel name EACH TIME! |
40
|
|
|
* |
41
|
|
|
* @param string $channel |
42
|
|
|
* @return $this |
43
|
|
|
* @access public |
44
|
|
|
* @api |
45
|
|
|
*/ |
46
|
|
|
public function __invoke(/*# string */ $channel) |
47
|
|
|
{ |
48
|
|
|
return $this->setChannel($channel); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Logs with an arbitrary level. |
53
|
|
|
* |
54
|
|
|
* @param mixed $level |
55
|
|
|
* @param string $message |
56
|
|
|
* @param array $context |
57
|
|
|
* @return null |
58
|
|
|
*/ |
59
|
|
|
public function log($level, $message, array $context = array()) |
60
|
|
|
{ |
61
|
|
|
// make sure channel is set with $logger($channel) |
62
|
|
|
$this->isChannelSet(); |
63
|
|
|
|
64
|
|
|
// create log entry |
65
|
|
|
$entry = $this->newLogEntry( |
66
|
|
|
$this->getChannel(), $level, $message, $context |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
// run processors |
70
|
|
|
$this->runProcessors($entry); |
71
|
|
|
|
72
|
|
|
// run handlers |
73
|
|
|
$this->runHandlers($entry); |
74
|
|
|
|
75
|
|
|
// mark channel unset |
76
|
|
|
$this->unsetChannel(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add handler to the channel with priority |
81
|
|
|
* |
82
|
|
|
* if $channel == '', use current logger channel |
83
|
|
|
* |
84
|
|
|
* @param callable $handler |
85
|
|
|
* @param string $channel channel to listen to |
86
|
|
|
* @param int $priority |
87
|
|
|
* @return $this |
88
|
|
|
* @access public |
89
|
|
|
* @api |
90
|
|
|
*/ |
91
|
|
|
public function addHandler( |
92
|
|
|
callable $handler, |
93
|
|
|
/*# string */ $channel = '', |
94
|
|
|
/*# int */ $priority = 0 |
95
|
|
|
) { |
96
|
|
|
return $this->addCallable('handlers', $handler, $channel, $priority); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Remove this handler from the channel |
101
|
|
|
* |
102
|
|
|
* if $channel == '', then remove this handler from all channels |
103
|
|
|
* |
104
|
|
|
* @param callable $handler |
105
|
|
|
* @param string $channel |
106
|
|
|
* @return $this |
107
|
|
|
* @access public |
108
|
|
|
* @api |
109
|
|
|
*/ |
110
|
|
|
public function removeHandler(callable $handler, $channel = '') |
111
|
|
|
{ |
112
|
|
|
return $this->removeCallable('handlers', $handler, $channel); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add processor to the channel with priority |
117
|
|
|
* |
118
|
|
|
* if $channel == '', use current logger channel |
119
|
|
|
* |
120
|
|
|
* @param callable $processor |
121
|
|
|
* @param string $channel channel to listen to |
122
|
|
|
* @param int $priority |
123
|
|
|
* @return $this |
124
|
|
|
* @access public |
125
|
|
|
* @api |
126
|
|
|
*/ |
127
|
|
|
public function addProcessor( |
128
|
|
|
callable $processor, |
129
|
|
|
/*# string */ $channel = '', |
130
|
|
|
/*# int */ $priority = 0 |
131
|
|
|
) { |
132
|
|
|
return $this->addCallable('processors', $processor, $channel, $priority); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Remove this $processor from $channel |
137
|
|
|
* |
138
|
|
|
* if $channel == '', then remove processor from all channels |
139
|
|
|
* |
140
|
|
|
* @param callable $processor |
141
|
|
|
* @param string $channel |
142
|
|
|
* @return $this |
143
|
|
|
* @access public |
144
|
|
|
* @api |
145
|
|
|
*/ |
146
|
|
|
public function removeProcessor(callable $processor, $channel = '') |
147
|
|
|
{ |
148
|
|
|
return $this->removeCallable('processors', $processor, $channel); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|