Skip to content

Commit

Permalink
Add logger functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
N1ghteyes committed Nov 14, 2019
1 parent f7f4f31 commit fcda1f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/apicore/interfaces/loggingInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace n1ghteyes\apicore\interfaces;

interface loggingInterface {
public function addMethod($method);
public function addRequestURL($method);
public function addRequestArgs($method);
public function addRequestEndpoint($method);
public function setRequestTime($time);

public function setResponseTime($time);
public function addRawResponse($rawResponse);
public function addResponseStatusCode($status);
}
21 changes: 21 additions & 0 deletions src/apicore/structure/apicore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use n1ghteyes\apicore\interfaces\coreInterface;
use GuzzleHttp;
use n1ghteyes\apicore\interfaces\loggingInterface;

/**
* PHP class to handle connections to any number of APIs. Config provided by YAML config file, Can be extended to account for differences in api structure.
Expand All @@ -26,6 +27,8 @@ abstract class apiCore implements coreInterface{
private $rawResponse = FALSE;
private $processedResponse = FALSE;
private $errors = array();
/** @var loggingInterface */
protected $logger;

/**
* apiCore constructor.
Expand All @@ -38,6 +41,10 @@ public function __construct()
$this->setBodyFormat();
}

public function addLogger(loggingInterface $logger){
$this->logger = $logger;
}

/**
* Function to set request Schema
* @param string $schema
Expand Down Expand Up @@ -183,6 +190,14 @@ public function __call($name, $arguments)
$this->processArgs($query);
$response = response::getInstance();
$response::verbUsed($this->httpMethod); //set the verb used for the request,
//do we have a logging class? If so, add data to it.
if(isset($this->logger)){
$this->logger->addMethod($this->httpMethod);
$this->logger->addRequestURL((string)$this->request);
$this->logger->addRequestArgs(json_encode($this->args));
$this->logger->addRequestEndpoint($name);
$this->logger->setRequestTime(time());
}
try {
$result = $client->request($this->httpMethod, (string)$this->request, $this->args);
//$this->processResult($result);
Expand All @@ -191,6 +206,12 @@ public function __call($name, $arguments)
$response::addError($e->getCode(), $e->getMessage());
}

if(isset($this->logger)){
$this->logger->setResponseTime(time());
$this->logger->addRawResponse($response->rawBodyData);
$this->logger->addResponseStatusCode($response->statusCode);
}

//reset some stuff post-query so we can handle the next one cleanly. Leave auth and headers in place by default.
$this->request->resetQueryString();
unset($this->args['query']);
Expand Down

0 comments on commit fcda1f7

Please sign in to comment.