Skip to content

Commit 8664cce

Browse files
committed
Beginning logging implementation
1 parent c17d068 commit 8664cce

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/HttpRequest.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class HttpRequest
3131
private $_Config;
3232

3333
/** @var string */
34-
private $_method;
34+
public $method;
3535
/** @var string */
36-
private $_path;
36+
public $path;
3737
/** @var string */
38-
private $_url;
38+
public $url;
3939

4040
/** @var array */
4141
private $_curlOpts = array();
@@ -59,10 +59,11 @@ class HttpRequest
5959
*/
6060
public function __construct($method, $path, Config $config, $body = null)
6161
{
62-
$this->params = new Params();
6362
$this->_Config = $config;
64-
$this->_method = strtolower($method);
65-
$this->_path = $path;
63+
64+
$this->params = new Params();
65+
$this->method = strtolower($method);
66+
$this->path = $path;
6667

6768
if ('' !== ($dc = $config->getDatacenter()))
6869
$this->params['dc'] = $dc;
@@ -131,9 +132,16 @@ public function setWriteOptions(WriteOptions $writeOptions = null)
131132
*/
132133
public function execute()
133134
{
134-
$this->_url = $this->_buildUrl();
135+
$this->url = sprintf(
136+
'%s/%s?%s',
137+
$this->_Config->compileAddress(),
138+
ltrim(trim($this->path), "/"),
139+
$this->params
140+
);
141+
142+
Logger::log('debug', 'Executing '.$this->method.' request '.$this->url.($this->body ? ' with body "'.$this->body.'"':''));
135143

136-
switch($this->_method)
144+
switch($this->method)
137145
{
138146
case 'get':
139147
// no prep needed
@@ -149,34 +157,41 @@ public function execute()
149157
return [null, new Error(sprintf(
150158
'%s - PHPConsulAPI currently does not support queries made using the "%s" method.',
151159
get_class($this),
152-
$this->_method
160+
$this->method
153161
))];
154162
}
155163

156-
$ch = curl_init($this->_url);
164+
$ch = curl_init($this->url);
157165

158166
if (false === $ch)
159167
{
160-
return [null, new Error(sprintf(
168+
$err = new Error(sprintf(
161169
'%s::execute - Unable to initialize CURL resource with URL "%s"',
162170
get_class($this),
163-
$this->_url
164-
))];
171+
$this->url
172+
));
173+
Logger::log('error', $err);
174+
return [null, $err];
165175
}
166176

167177
if (false === curl_setopt_array($ch, $this->_curlOpts))
168178
{
169-
return [null, new Error(sprintf(
179+
$err = new Error(sprintf(
170180
'%s - Unable to set specified Curl options, please ensure you\'re passing in valid constants. Specified options: %s',
171181
get_class($this),
172182
json_encode($this->_curlOpts)
173-
))];
183+
));
184+
Logger::log('error', $err);
185+
return [null, $err];
174186
}
175187

176188
$response = new HttpResponse(curl_exec($ch), curl_getinfo($ch), curl_error($ch));
177189

178190
curl_close($ch);
179191

192+
193+
Logger::log('debug', 'Response received - Code: '.$response->httpCode.'; Body: '.$response->body.'; ');
194+
180195
return [$response, null];
181196
}
182197

@@ -217,17 +232,4 @@ private function _compileBody()
217232
return '';
218233
}
219234
}
220-
221-
/**
222-
* @return string
223-
*/
224-
private function _buildUrl()
225-
{
226-
return sprintf(
227-
'%s/%s?%s',
228-
$this->_Config->compileAddress(),
229-
ltrim(trim($this->_path), "/"),
230-
$this->params
231-
);
232-
}
233235
}

var/logs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.gitignore

0 commit comments

Comments
 (0)