Skip to content

Commit 124c0c9

Browse files
committed
Experimenting with logger...will probably change it.
1 parent 2b79b54 commit 124c0c9

File tree

10 files changed

+554
-35
lines changed

10 files changed

+554
-35
lines changed

src/AbstractConsulClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function requireOK(array $requestResult)
5555

5656
if (200 !== $response->httpCode)
5757
{
58-
return [$duration, $response, new Error('warn', sprintf(
58+
return [$duration, $response, new Error(sprintf(
5959
'%s - Error seen while executing "%s". Response code: %d. Message: %s',
6060
get_class($this),
6161
$response->url,
@@ -142,7 +142,7 @@ protected function decodeBody(HttpResponse $response)
142142
if (JSON_ERROR_NONE === $err)
143143
return [$data, null];
144144

145-
return [null, new Error('error', sprintf(
145+
return [null, new Error(sprintf(
146146
'%s - Unable to parse response as JSON. Message: %s',
147147
get_class($this),
148148
PHP_VERSION_ID >= 50500 ? json_last_error_msg() : (string)$err

src/Config.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
*/
2323
class Config extends AbstractDefinedStrictCollection
2424
{
25+
/** @var array */
26+
private $_logLevels = array(
27+
'debug' => 0,
28+
'info' => 1,
29+
'warn' => 2,
30+
'error' => 3,
31+
);
32+
2533
/**
2634
* @return static
2735
*/
@@ -66,6 +74,8 @@ protected function getDefinition()
6674
'CertFile' => null,
6775
'KeyFile' => null,
6876
'InsecureSkipVerify' => null,
77+
'LogLevel' => 'warn',
78+
'Loggers' => array(),
6979
);
7080
}
7181

@@ -274,6 +284,80 @@ public function setInsecureSkipVerify($insecureSkipVerify)
274284
return $this;
275285
}
276286

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+
277361
/**
278362
* @return string
279363
*/
@@ -312,6 +396,22 @@ public function getCurlOptArray()
312396
return $opts;
313397
}
314398

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+
315415
/**
316416
* @param string $param
317417
* @return string|bool

src/ConsulAPILoggerInterface.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php namespace DCarbone\PHPConsulAPI;
2+
3+
/*
4+
Copyright 2016 Daniel Carbone ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
/**
20+
* Interface ConsulAPILoggerInterface
21+
* @package DCarbone\PHPConsulAPI
22+
*/
23+
interface ConsulAPILoggerInterface
24+
{
25+
/**
26+
* @param string $message
27+
* @return bool
28+
*/
29+
public function error($message);
30+
31+
/**
32+
* @param string $message
33+
* @return bool
34+
*/
35+
public function warn($message);
36+
37+
/**
38+
* @param string $message
39+
* @return bool
40+
*/
41+
public function info($message);
42+
43+
/**
44+
* @param string $message
45+
* @return bool
46+
*/
47+
public function debug($message);
48+
}

src/DateTime.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public function formatDefault()
6565
return $this->format(self::$_defaultFormat);
6666
}
6767

68+
/**
69+
* @return string
70+
*/
71+
public static function now()
72+
{
73+
return date(self::$_defaultFormat);
74+
}
75+
6876
/**
6977
* Specify data which should be serialized to JSON
7078
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php

src/Error.php

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424
*/
2525
class Error implements \JsonSerializable
2626
{
27-
/** @var array */
28-
private $_levels = array(
29-
'debug',
30-
'info',
31-
'warn',
32-
'error',
33-
);
34-
35-
/** @var string */
36-
private $_level;
37-
3827
/** @var DateTime */
3928
private $_timestamp;
4029

@@ -43,22 +32,11 @@ class Error implements \JsonSerializable
4332

4433
/**
4534
* Error constructor.
46-
* @param string $level
4735
* @param string $message
36+
* @internal param string $level
4837
*/
49-
public function __construct($level, $message)
38+
public function __construct($message)
5039
{
51-
if (!is_string($level) || !in_array($level, $this->_levels, true))
52-
{
53-
throw new \InvalidArgumentException(sprintf(
54-
'%s - "%s" is not valid level. Available choices: ["%s"]',
55-
get_class($this),
56-
is_string($level) ? $level : gettype($level),
57-
implode('", "', $this->_levels)
58-
));
59-
}
60-
61-
$this->_level = $level;
6240
$this->_timestamp = new DateTime();
6341
$this->_message = $message;
6442
}
@@ -71,7 +49,6 @@ public function __construct($level, $message)
7149
public function jsonSerialize()
7250
{
7351
return array(
74-
'level' => $this->_level,
7552
'message' => $this->_message,
7653
'timestamp' => $this->_timestamp
7754
);
@@ -83,8 +60,7 @@ public function jsonSerialize()
8360
public function __toString()
8461
{
8562
return sprintf(
86-
'[%s] - %s - %s',
87-
$this->_level,
63+
'[error] - %s - %s',
8864
$this->_timestamp,
8965
$this->_message
9066
);

0 commit comments

Comments
 (0)