|
22 | 22 | */ |
23 | 23 | class Config extends AbstractDefinedStrictCollection |
24 | 24 | { |
| 25 | + /** @var array */ |
| 26 | + private $_logLevels = array( |
| 27 | + 'debug' => 0, |
| 28 | + 'info' => 1, |
| 29 | + 'warn' => 2, |
| 30 | + 'error' => 3, |
| 31 | + ); |
| 32 | + |
25 | 33 | /** |
26 | 34 | * @return static |
27 | 35 | */ |
@@ -66,6 +74,8 @@ protected function getDefinition() |
66 | 74 | 'CertFile' => null, |
67 | 75 | 'KeyFile' => null, |
68 | 76 | 'InsecureSkipVerify' => null, |
| 77 | + 'LogLevel' => 'warn', |
| 78 | + 'Loggers' => array(), |
69 | 79 | ); |
70 | 80 | } |
71 | 81 |
|
@@ -274,6 +284,80 @@ public function setInsecureSkipVerify($insecureSkipVerify) |
274 | 284 | return $this; |
275 | 285 | } |
276 | 286 |
|
| 287 | + /** |
| 288 | + * @return ConsulAPILoggerInterface[] |
| 289 | + */ |
| 290 | + public function getLoggers() |
| 291 | + { |
| 292 | + return $this->_storage['Loggers']; |
| 293 | + } |
| 294 | + |
| 295 | + /** |
| 296 | + * @param array $loggers |
| 297 | + * @return $this |
| 298 | + */ |
| 299 | + public function setLoggers(array $loggers) |
| 300 | + { |
| 301 | + $this->_storage['Loggers'] = array(); |
| 302 | + foreach($loggers as $logger) |
| 303 | + { |
| 304 | + if ($logger instanceof ConsulAPILoggerInterface) |
| 305 | + { |
| 306 | + $this->_storage['Loggers'][] = $logger; |
| 307 | + } |
| 308 | + else |
| 309 | + { |
| 310 | + throw new \InvalidArgumentException(sprintf( |
| 311 | + '%s - %s is not a valid logger implementation', |
| 312 | + get_class($this), |
| 313 | + is_object($logger) ? get_class($logger) : gettype($logger) |
| 314 | + )); |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + return $this; |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * @param ConsulAPILoggerInterface $logger |
| 323 | + * @return $this |
| 324 | + */ |
| 325 | + public function addLogger(ConsulAPILoggerInterface $logger) |
| 326 | + { |
| 327 | + $this->_storage['Loggers'][] = $logger; |
| 328 | + |
| 329 | + return $this; |
| 330 | + } |
| 331 | + |
| 332 | + /** |
| 333 | + * @return string |
| 334 | + */ |
| 335 | + public function getLogLevel() |
| 336 | + { |
| 337 | + return $this->_storage['LogLevel']; |
| 338 | + } |
| 339 | + |
| 340 | + /** |
| 341 | + * @param string $logLevel |
| 342 | + * @return $this |
| 343 | + */ |
| 344 | + public function setLogLevel($logLevel) |
| 345 | + { |
| 346 | + if (!is_string($logLevel) || '' === ($level = strtolower($logLevel)) || !isset($this->_logLevels[$level])) |
| 347 | + { |
| 348 | + throw new \InvalidArgumentException(sprintf( |
| 349 | + '%s - Log level must be one of the following values: ["%s"]. %s seen.', |
| 350 | + get_class($this), |
| 351 | + implode('", "', array_keys($this->_logLevels)), |
| 352 | + is_string($logLevel) ? $logLevel : gettype($logLevel) |
| 353 | + )); |
| 354 | + } |
| 355 | + |
| 356 | + $this->_storage['LogLevel'] = $level; |
| 357 | + |
| 358 | + return $this; |
| 359 | + } |
| 360 | + |
277 | 361 | /** |
278 | 362 | * @return string |
279 | 363 | */ |
@@ -312,6 +396,22 @@ public function getCurlOptArray() |
312 | 396 | return $opts; |
313 | 397 | } |
314 | 398 |
|
| 399 | + /** |
| 400 | + * @param string $logLevel |
| 401 | + * @param string $message |
| 402 | + */ |
| 403 | + public function log($logLevel, $message) |
| 404 | + { |
| 405 | + $level = strtolower($logLevel); |
| 406 | + if ($this->_logLevels[$this->_storage['LogLevel']] <= $this->_logLevels[strtolower($level)]) |
| 407 | + { |
| 408 | + foreach($this->_storage['Loggers'] as $logger) |
| 409 | + { |
| 410 | + $logger->{$level}($message); |
| 411 | + } |
| 412 | + } |
| 413 | + } |
| 414 | + |
315 | 415 | /** |
316 | 416 | * @param string $param |
317 | 417 | * @return string|bool |
|
0 commit comments