Skip to content

Commit c17d068

Browse files
committed
Changing logging system a bit...
1 parent 0146548 commit c17d068

File tree

2 files changed

+155
-118
lines changed

2 files changed

+155
-118
lines changed

src/Config.php

Lines changed: 18 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,24 @@
2222
*/
2323
class Config extends AbstractStrictCollection
2424
{
25-
/** @var array */
26-
private $_logLevels = array(
27-
'debug' => 0,
28-
'info' => 1,
29-
'warn' => 2,
30-
'error' => 3,
31-
);
25+
/**
26+
* @return array
27+
*/
28+
protected function getDefinition()
29+
{
30+
return array(
31+
'Address' => null,
32+
'Scheme' => null,
33+
'Datacenter' => null,
34+
'HttpAuth' => null,
35+
'WaitTime' => null,
36+
'Token' => null,
37+
'CAFile' => null,
38+
'CertFile' => null,
39+
'KeyFile' => null,
40+
'InsecureSkipVerify' => null
41+
);
42+
}
3243

3344
/**
3445
* @return static
@@ -58,27 +69,6 @@ public static function newDefaultConfig()
5869
return $conf;
5970
}
6071

61-
/**
62-
* @return array
63-
*/
64-
protected function getDefinition()
65-
{
66-
return array(
67-
'Address' => null,
68-
'Scheme' => null,
69-
'Datacenter' => null,
70-
'HttpAuth' => null,
71-
'WaitTime' => null,
72-
'Token' => null,
73-
'CAFile' => null,
74-
'CertFile' => null,
75-
'KeyFile' => null,
76-
'InsecureSkipVerify' => null,
77-
'LogLevel' => 'warn',
78-
'Loggers' => array(),
79-
);
80-
}
81-
8272
/**
8373
* @return string
8474
*/
@@ -284,80 +274,6 @@ public function setInsecureSkipVerify($insecureSkipVerify)
284274
return $this;
285275
}
286276

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-
361277
/**
362278
* @return string
363279
*/
@@ -396,22 +312,6 @@ public function getCurlOptArray()
396312
return $opts;
397313
}
398314

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-
415315
/**
416316
* @param string $param
417317
* @return string|bool

src/Logger.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
* Class Logger
21+
* @package DCarbone\PHPConsulAPI
22+
*/
23+
abstract class Logger
24+
{
25+
/** @var array */
26+
private static $_loggers = array();
27+
28+
/** @var FileLogger */
29+
private static $_defaultLogger = null;
30+
31+
/** @var bool */
32+
private static $_initialized = false;
33+
34+
/** @var string */
35+
private static $_logLevel = 'warn';
36+
37+
/** @var array */
38+
private static $_logLevels = array(
39+
'debug' => 0,
40+
'info' => 1,
41+
'warn' => 2,
42+
'error' => 3,
43+
);
44+
45+
public static function init()
46+
{
47+
if (self::$_initialized)
48+
return;
49+
50+
self::$_defaultLogger = new FileLogger(__DIR__.'/../var/logs/php-consul-api.log');
51+
52+
self::$_initialized = true;
53+
}
54+
55+
public static function removeDefaultLogger()
56+
{
57+
self::$_defaultLogger = null;
58+
}
59+
60+
/**
61+
* @param string $logLevel
62+
*/
63+
public static function setLogLevel($logLevel)
64+
{
65+
if (!is_string($logLevel) || '' === ($level = strtolower($logLevel)) || !isset(self::$_logLevels[$level]))
66+
{
67+
throw new \InvalidArgumentException(sprintf(
68+
'%s - Log level must be one of the following values: ["%s"]. %s seen.',
69+
get_called_class(),
70+
implode('", "', array_keys(self::$_logLevels)),
71+
is_string($logLevel) ? $logLevel : gettype($logLevel)
72+
));
73+
}
74+
75+
self::$_logLevel = $level;
76+
}
77+
78+
/**
79+
* @param ConsulAPILoggerInterface[] $loggers
80+
*/
81+
public static function setLoggers(array $loggers)
82+
{
83+
self::$_loggers = array();
84+
self::$_defaultLogger = null;
85+
foreach($loggers as $logger)
86+
{
87+
if ($logger instanceof ConsulAPILoggerInterface)
88+
{
89+
self::$_loggers[] = $logger;
90+
}
91+
else
92+
{
93+
throw new \InvalidArgumentException(sprintf(
94+
'%s - %s is not a valid logger implementation',
95+
get_called_class(),
96+
is_object($logger) ? get_class($logger) : gettype($logger)
97+
));
98+
}
99+
}
100+
}
101+
102+
/**
103+
* @param ConsulAPILoggerInterface $logger
104+
*/
105+
public static function addLogger(ConsulAPILoggerInterface $logger)
106+
{
107+
self::$_loggers[] = $logger;
108+
}
109+
110+
/**
111+
* Clear out all loggers
112+
*/
113+
public static function resetLoggers()
114+
{
115+
self::$_loggers = array();
116+
}
117+
118+
/**
119+
* @param string $logLevel
120+
* @param string $message
121+
*/
122+
public static function log($logLevel, $message)
123+
{
124+
$level = strtolower($logLevel);
125+
if (self::$_logLevels[self::$_logLevel] <= self::$_logLevels[strtolower($level)])
126+
{
127+
if (self::$_defaultLogger)
128+
self::$_defaultLogger->{$level}($message);
129+
130+
foreach(self::$_loggers as $logger)
131+
{
132+
$logger->{$level}($message);
133+
}
134+
}
135+
}
136+
}
137+
Logger::init();

0 commit comments

Comments
 (0)