1
|
|
|
<?php namespace Comodojo\Dispatcher\Components;
|
2
|
|
|
|
3
|
|
|
use \Monolog\Handler\HandlerInterface;
|
4
|
|
|
use \Monolog\Logger;
|
5
|
|
|
use \Monolog\Handler\StreamHandler;
|
6
|
|
|
use \Monolog\Handler\SyslogHandler;
|
7
|
|
|
use \Monolog\Handler\ErrorLogHandler;
|
8
|
|
|
use \Monolog\Handler\NullHandler;
|
9
|
|
|
use \Comodojo\Dispatcher\Components\Configuration;
|
10
|
|
|
|
11
|
|
|
/**
|
12
|
|
|
* @package Comodojo Dispatcher
|
13
|
|
|
* @author Marco Giovinazzi <[email protected]>
|
14
|
|
|
* @author Marco Castiello <[email protected]>
|
15
|
|
|
* @license GPL-3.0+
|
16
|
|
|
*
|
17
|
|
|
* LICENSE:
|
18
|
|
|
*
|
19
|
|
|
* This program is free software: you can redistribute it and/or modify
|
20
|
|
|
* it under the terms of the GNU Affero General Public License as
|
21
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
22
|
|
|
* License, or (at your option) any later version.
|
23
|
|
|
*
|
24
|
|
|
* This program is distributed in the hope that it will be useful,
|
25
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
26
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
27
|
|
|
* GNU Affero General Public License for more details.
|
28
|
|
|
*
|
29
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
30
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
31
|
|
|
*/
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class LogManager {
|
35
|
|
|
|
36
|
|
|
public $configuration;
|
37
|
|
|
|
38
|
|
|
public $name;
|
39
|
|
|
|
40
|
|
|
private static $levels = array(
|
41
|
|
|
'INFO' => Logger::INFO,
|
42
|
|
|
'NOTICE' => Logger::NOTICE,
|
43
|
|
|
'WARNING' => Logger::WARNING,
|
44
|
|
|
'ERROR' => Logger::ERROR,
|
45
|
|
|
'CRITICAL' => Logger::CRITICAL,
|
46
|
|
|
'ALERT' => Logger::ALERT,
|
47
|
|
|
'EMERGENCY' => Logger::EMERGENCY,
|
48
|
|
|
'DEBUG' => Logger::DEBUG
|
49
|
|
|
);
|
50
|
|
|
|
51
|
2 |
|
public function __construct(Configuration $configuration, $name = 'dispatcher') {
|
52
|
|
|
|
53
|
2 |
|
$this->configuration = $configuration;
|
54
|
2 |
|
$this->name = $name;
|
55
|
|
|
|
56
|
2 |
|
}
|
57
|
|
|
|
58
|
2 |
|
public function init() {
|
59
|
|
|
|
60
|
2 |
|
$log = $this->configuration->get('log');
|
61
|
|
|
|
62
|
2 |
|
$name = empty($log['name']) ? $this->name : $log['name'];
|
63
|
|
|
|
64
|
2 |
|
$logger = new Logger($name);
|
65
|
|
|
|
66
|
|
|
if (
|
67
|
2 |
|
empty($log) ||
|
68
|
2 |
|
( isset($log['enabled']) && $log['enabled'] === false ) ||
|
69
|
2 |
|
empty($log['providers'])
|
70
|
2 |
|
) {
|
71
|
|
|
|
72
|
|
|
$logger->pushHandler( new NullHandler( self::getLevel() ) );
|
73
|
|
|
|
74
|
|
|
} else {
|
75
|
|
|
|
76
|
2 |
|
foreach ($log['providers'] as $provider => $parameters) {
|
77
|
|
|
|
78
|
2 |
|
$handler = $this->getHandler($provider, $parameters);
|
79
|
|
|
|
80
|
2 |
|
if ( $handler instanceof HandlerInterface ) $logger->pushHandler($handler);
|
81
|
|
|
|
82
|
2 |
|
}
|
83
|
|
|
|
84
|
|
|
}
|
85
|
|
|
|
86
|
2 |
|
return $logger;
|
87
|
|
|
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* Create the logger
|
92
|
|
|
*
|
93
|
|
|
* @param Configuration $configuration
|
94
|
|
|
*
|
95
|
|
|
* @return Logger
|
96
|
|
|
*/
|
97
|
2 |
|
public static function create(Configuration $configuration) {
|
98
|
|
|
|
99
|
2 |
|
$log = new LogManager($configuration);
|
100
|
|
|
|
101
|
2 |
|
return $log->init();
|
102
|
|
|
|
103
|
|
|
}
|
104
|
|
|
|
105
|
2 |
|
protected function getHandler($provider, $parameters) {
|
106
|
|
|
|
107
|
2 |
|
switch ( $parameters['type'] ) {
|
108
|
|
|
|
109
|
2 |
|
case 'StreamHandler':
|
110
|
2 |
|
$handler = $this->getStreamHandler($provider, $parameters);
|
111
|
2 |
|
break;
|
112
|
|
|
|
113
|
|
|
case 'SyslogHandler':
|
114
|
|
|
$handler = $this->getSyslogHandler($provider, $parameters);
|
115
|
|
|
break;
|
116
|
|
|
|
117
|
|
|
case 'ErrorLogHandler':
|
118
|
|
|
$handler = $this->getErrorLogHandler($provider, $parameters);
|
119
|
|
|
break;
|
120
|
|
|
|
121
|
|
|
default:
|
122
|
|
|
$handler = null;
|
123
|
|
|
break;
|
124
|
|
|
|
125
|
2 |
|
}
|
126
|
|
|
|
127
|
2 |
|
return $handler;
|
128
|
|
|
|
129
|
|
|
}
|
130
|
|
|
|
131
|
2 |
|
protected function getStreamHandler($name, $parameters) {
|
|
|
|
|
132
|
|
|
|
133
|
2 |
|
$stream = $this->configuration->get('base-path').'/'.(empty($parameters['stream']) ? 'dispatcher.log' : $parameters['stream']);
|
134
|
|
|
|
135
|
2 |
|
$level = self::getLevel( empty($parameters['level']) ? null : $parameters['level'] );
|
136
|
|
|
|
137
|
2 |
|
$bubble = self::getBubble( empty($parameters['bubble']) ? true : $parameters['bubble'] );
|
138
|
|
|
|
139
|
2 |
|
$filePermission = self::getFilePermission( empty($parameters['filePermission']) ? null : $parameters['filePermission'] );
|
140
|
|
|
|
141
|
2 |
|
$useLocking = self::getLocking( empty($parameters['useLocking']) ? false : $parameters['useLocking'] );
|
142
|
|
|
|
143
|
2 |
|
return new StreamHandler($stream, $level, $bubble, $filePermission, $useLocking);
|
144
|
|
|
|
145
|
|
|
}
|
146
|
|
|
|
147
|
|
|
protected function getSyslogHandler($name, $parameters) {
|
|
|
|
|
148
|
|
|
|
149
|
|
|
if ( empty($parameters['ident']) ) return null;
|
150
|
|
|
|
151
|
|
|
$facility = empty($parameters['facility']) ? LOG_USER : $parameters['facility'];
|
152
|
|
|
|
153
|
|
|
$level = self::getLevel( empty($parameters['level']) ? null : $parameters['level'] );
|
154
|
|
|
|
155
|
|
|
$bubble = self::getBubble( empty($parameters['bubble']) ? true : $parameters['bubble'] );
|
156
|
|
|
|
157
|
|
|
$logopts = empty($parameters['logopts']) ? LOG_PID : $parameters['logopts'];
|
158
|
|
|
|
159
|
|
|
return new SyslogHandler($parameters['ident'], $facility, $level, $bubble, $logopts);
|
160
|
|
|
|
161
|
|
|
}
|
162
|
|
|
|
163
|
|
|
protected function getErrorLogHandler($name, $parameters) {
|
|
|
|
|
164
|
|
|
|
165
|
|
|
$messageType = empty($parameters['messageType']) ? ErrorLogHandler::OPERATING_SYSTEM : $parameters['messageType'];
|
166
|
|
|
|
167
|
|
|
$level = self::getLevel( empty($parameters['level']) ? null : $parameters['level'] );
|
168
|
|
|
|
169
|
|
|
$bubble = self::getBubble( empty($parameters['bubble']) ? true : $parameters['bubble'] );
|
170
|
|
|
|
171
|
|
|
$expandNewlines = self::getExpandNewlines( empty($parameters['expandNewlines']) ? false : $parameters['expandNewlines'] );
|
172
|
|
|
|
173
|
|
|
return new ErrorLogHandler($messageType, $level, $bubble, $expandNewlines);
|
174
|
|
|
|
175
|
|
|
}
|
176
|
|
|
|
177
|
|
|
/**
|
178
|
|
|
* Map provided log level to level code
|
179
|
|
|
*
|
180
|
|
|
* @param string $level
|
181
|
|
|
*
|
182
|
|
|
* @return integer
|
183
|
|
|
*/
|
184
|
2 |
|
protected static function getLevel($level = null) {
|
185
|
|
|
|
186
|
2 |
|
$level = strtoupper($level);
|
187
|
|
|
|
188
|
2 |
|
if ( array_key_exists($level, self::$levels) ) return self::$levels[$level];
|
189
|
|
|
|
190
|
|
|
return self::$levels['DEBUG'];
|
191
|
|
|
|
192
|
|
|
}
|
193
|
|
|
|
194
|
2 |
|
protected static function getBubble($bubble) {
|
195
|
|
|
|
196
|
2 |
|
return filter_var($bubble, FILTER_VALIDATE_BOOLEAN, array(
|
197
|
|
|
'options' => array(
|
198
|
|
|
'default' => true
|
199
|
2 |
|
)
|
200
|
2 |
|
));
|
201
|
|
|
|
202
|
|
|
}
|
203
|
|
|
|
204
|
2 |
|
protected static function getFilePermission($filepermission = null) {
|
205
|
|
|
|
206
|
2 |
|
if ( is_null($filepermission) ) return null;
|
207
|
|
|
|
208
|
|
|
return filter_var($filepermission, FILTER_VALIDATE_INT, array(
|
209
|
|
|
'options' => array(
|
210
|
|
|
'default' => 0644
|
211
|
|
|
),
|
212
|
|
|
'flags' => FILTER_FLAG_ALLOW_OCTAL
|
213
|
|
|
));
|
214
|
|
|
|
215
|
|
|
}
|
216
|
|
|
|
217
|
2 |
|
protected static function getLocking($uselocking) {
|
218
|
|
|
|
219
|
2 |
|
return filter_var($uselocking, FILTER_VALIDATE_BOOLEAN, array(
|
220
|
|
|
'options' => array(
|
221
|
|
|
'default' => false
|
222
|
2 |
|
)
|
223
|
2 |
|
));
|
224
|
|
|
|
225
|
|
|
}
|
226
|
|
|
|
227
|
|
|
protected static function getExpandNewlines($expandNewlines) {
|
228
|
|
|
|
229
|
|
|
return filter_var($expandNewlines, FILTER_VALIDATE_BOOLEAN, array(
|
230
|
|
|
'options' => array(
|
231
|
|
|
'default' => false
|
232
|
|
|
)
|
233
|
|
|
));
|
234
|
|
|
|
235
|
|
|
}
|
236
|
|
|
|
237
|
|
|
}
|
238
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.