Better logging
This commit is contained in:
@@ -10,68 +10,40 @@ abstract class WebService
|
||||
private $func = null;
|
||||
private $status = 200;
|
||||
|
||||
private $log = '';
|
||||
|
||||
/**
|
||||
* Log a message that will be displayed in the logs if the configuration
|
||||
* says so.
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $verbosity
|
||||
* @param bool $withTime
|
||||
*/
|
||||
public function log($message, $verbosity = 1, $withTime = false) {
|
||||
if(Configuration::get('log.verbosity') < $verbosity) {
|
||||
return;
|
||||
}
|
||||
|
||||
if($withTime) {
|
||||
$message = date("d-m-Y h:m:s").' '.$message;
|
||||
}
|
||||
|
||||
$this->log .= $message."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Treat the current request and output the result. This is the only
|
||||
* method that should be called on the webservice directly !
|
||||
*/
|
||||
public function Run()
|
||||
{
|
||||
Logger::start();
|
||||
|
||||
$renderer = new Renderer();
|
||||
|
||||
$this->log("------------------");
|
||||
$this->log("Start request", 1, true);
|
||||
$data = array();
|
||||
|
||||
try {
|
||||
$result = $this->Call();
|
||||
$data["result"][$this->func] = $result;
|
||||
|
||||
Logger::log(print_r($result, true));
|
||||
} catch (WebException $e) {
|
||||
$data["error"]["code"] = $e->getCode();
|
||||
$data["error"]["reason"] = $e->getMessage();
|
||||
$data["error"]["name"] = $e->getName();
|
||||
$this->status = 400;
|
||||
|
||||
$this->log(sprintf("Error : [%s] %s", $e->getCode(), $e->getName()));
|
||||
Logger::info($e->getName(), 'error');
|
||||
} catch (\Exception $e) {
|
||||
$data["failure"]["code"] = $e->getCode();
|
||||
$data["failure"]["reason"] = $e->getMessage();
|
||||
$this->status = 500;
|
||||
|
||||
$this->log(sprintf("Failure : %s", $e->getMessage()));
|
||||
Logger::info($e->getMessage(), 'error');
|
||||
}
|
||||
|
||||
$this->log("Data: ".print_r($data, true), 2);
|
||||
|
||||
Logger::stop(array('status' => $this->status));
|
||||
$renderer->render($this->status, $data);
|
||||
|
||||
$this->log("Request finished", 1, true);
|
||||
$this->log("------------------\n\n");
|
||||
|
||||
if(Configuration::get('log.verbosity') > 0) {
|
||||
file_put_contents(Configuration::get('log.file'), $this->log, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,6 +70,10 @@ abstract class WebService
|
||||
$this->func = $params["func"];
|
||||
unset($params['func']);
|
||||
|
||||
Logger::info(array(
|
||||
'func' => $this->func.'('.implode(', ', $params).')',
|
||||
));
|
||||
|
||||
if (!is_callable(array($this, $this->func))) {
|
||||
throw new UsageException("BadMethod", "Method {$this->func} does not exists.", UsageException::BAD_METHOD);
|
||||
}
|
||||
@@ -115,8 +91,6 @@ abstract class WebService
|
||||
throw new UsageException("TooManyArgs", "You must provide at most $nbArgs arguments.", UsageException::TOO_MANY_ARGS);
|
||||
}
|
||||
|
||||
$this->log("Calling '".$this->func."'");
|
||||
$this->log("Params: ".print_r($params, true), 2);
|
||||
return call_user_func_array(array($this, $this->func), $params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user