|
1
|
|
|
<?php namespace Comodojo\Daemon\Console; |
|
2
|
|
|
|
|
3
|
|
|
use \Monolog\Logger; |
|
4
|
|
|
use \Monolog\Handler\AbstractProcessingHandler; |
|
5
|
|
|
use \League\CLImate\CLImate; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @package Comodojo Daemon |
|
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* |
|
12
|
|
|
* LICENSE: |
|
13
|
|
|
* |
|
14
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
15
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
16
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
17
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
18
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
19
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
20
|
|
|
* THE SOFTWARE. |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
class LogHandler extends AbstractProcessingHandler { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var bool |
|
27
|
|
|
*/ |
|
28
|
|
|
private $include_context = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var CLImate |
|
32
|
|
|
*/ |
|
33
|
|
|
private $outputcontroller; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Colors to log level mapping |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $colors = [ |
|
41
|
|
|
100 => 'light_green', |
|
42
|
|
|
200 => 'green', |
|
43
|
|
|
250 => 'light_yellow', |
|
44
|
|
|
300 => 'yellow', |
|
45
|
|
|
400 => 'light_red', |
|
46
|
|
|
500 => 'red', |
|
47
|
|
|
550 => 'light_magenta', |
|
48
|
|
|
600 => 'magenta', |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Class constructor |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($level = Logger::DEBUG, $bubble = true) { |
|
55
|
|
|
|
|
56
|
|
|
$this->outputcontroller = new CLImate(); |
|
57
|
|
|
|
|
58
|
|
|
parent::__construct($level, $bubble); |
|
59
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Turn on context writer |
|
64
|
|
|
* |
|
65
|
|
|
* @return LogHandler |
|
66
|
|
|
*/ |
|
67
|
|
|
public function includeContext() { |
|
68
|
|
|
|
|
69
|
|
|
$this->include_context = true; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Turn off context writer |
|
77
|
|
|
* |
|
78
|
|
|
* @return LogHandler |
|
79
|
|
|
*/ |
|
80
|
|
|
public function excludeContext() { |
|
81
|
|
|
|
|
82
|
|
|
$this->include_context = false; |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Record's writer |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function write(array $record) { |
|
92
|
|
|
|
|
93
|
|
|
$level = $record['level']; |
|
94
|
|
|
|
|
95
|
|
|
$message = $record['formatted']; |
|
96
|
|
|
|
|
97
|
|
|
$context = empty($record['context']) ? null : $record['context']; |
|
98
|
|
|
|
|
99
|
|
|
$time = $record['datetime']->format('c'); |
|
100
|
|
|
|
|
101
|
|
|
$this->toConsole($time, $level, $message, $context); |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Send record to console formatting it |
|
107
|
|
|
*/ |
|
108
|
|
|
private function toConsole($time, $level, $message, $context) { |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
$color = static::$colors[$level]; |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$pattern = "<%s>%s</%s>"; |
|
113
|
|
|
|
|
114
|
|
|
$message = sprintf($pattern, $color, $message, $color); |
|
115
|
|
|
|
|
116
|
|
|
$this->outputcontroller->out($message); |
|
117
|
|
|
|
|
118
|
|
|
if ( !empty($context) && $this->include_context ) { |
|
119
|
|
|
|
|
120
|
|
|
$this->outputcontroller->out(sprintf($pattern, $color, var_export($context, true), $color)); |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.