summaryrefslogtreecommitdiffstats
path: root/admin/WebConsole/rest
diff options
context:
space:
mode:
authorramon <ramongomez@us.es>2017-10-17 12:30:41 +0000
committerramon <ramongomez@us.es>2017-10-17 12:30:41 +0000
commit13dc95973129473d1fe45ac7b024619cd7156b97 (patch)
tree9e3057fd93e6e11967afd45556d3a09c3c94a2b0 /admin/WebConsole/rest
parenta06dbacd08a0c9dbd98b3110a728f7b1e24b07f7 (diff)
#708: AƱadir mensajes de log al inicio/fin de cada ruta REST.
git-svn-id: https://opengnsys.es/svn/branches/version1.1@5467 a21b9725-9963-47de-94b9-378ad31fedc9
Diffstat (limited to 'admin/WebConsole/rest')
-rw-r--r--admin/WebConsole/rest/common.php44
-rw-r--r--admin/WebConsole/rest/index.php2
2 files changed, 42 insertions, 4 deletions
diff --git a/admin/WebConsole/rest/common.php b/admin/WebConsole/rest/common.php
index b7dcd109..12cbfe23 100644
--- a/admin/WebConsole/rest/common.php
+++ b/admin/WebConsole/rest/common.php
@@ -11,9 +11,32 @@
*/
+// Common constants.
+define('REST_LOGFILE', '/opt/opengnsys/log/rest.log');
+
+
// Common functions.
/**
+ * @brief Function to write a line into log file.
+ * @param string message Message to log.
+ * warning Line format: "Date: ClientIP: UserId: Status: Method Route: Message"
+ */
+function writeRestLog($message = "") {
+ global $userid;
+ if (is_writable(REST_LOGFILE)) {
+ $app = \Slim\Slim::getInstance();
+ file_put_contents(REST_LOGFILE, date(DATE_ISO8601) .": " .
+ $_SERVER['REMOTE_ADDR'] . ": " .
+ (isset($userid) ? $userid : "-") . ": " .
+ $app->response->getStatus() . ": " .
+ $app->request->getMethod() . " " .
+ $app->request->getPathInfo() . ": $message\n",
+ FILE_APPEND);
+ }
+}
+
+/**
* @brief Compose JSON response.
* @param int status Status code for HTTP response.
* @param array response Response data.
@@ -177,19 +200,34 @@ $app->notFound(function() {
);
/**
- * @brief Hook to write an error log message.
- * @warning Message will be written in web server's error file.
+ * @brief Hook to write a REST init log message, if debug is enabled.
+ * @warning Message will be written in REST log file.
+ */
+$app->hook('slim.before', function() use ($app) {
+ if ($app->settings['debug'])
+ writeRestLog("Init.");
+ }
+);
+
+/**
+ * @brief Hook to write an error log message and a REST exit log message if debug is enabled.
+ * @warning Error message will be written in web server's error file.
+ * @warning REST message will be written in REST log file.
*/
$app->hook('slim.after', function() use ($app) {
if ($app->response->getStatus() != 200 ) {
// Compose error message (truncating long lines).
$app->log->error(date(DATE_ISO8601) . ': ' .
- $app->getName() . ' ' .
+ $app->getName() . ': ' .
+ $_SERVER['REMOTE_ADDR'] . ": " .
+ (isset($userid) ? $userid : "-") . ": " .
$app->response->getStatus() . ': ' .
$app->request->getMethod() . ' ' .
$app->request->getPathInfo() . ': ' .
substr($app->response->getBody(), 0, 100));
}
+ if ($app->settings['debug'])
+ writeRestLog("Exit.");
}
);
diff --git a/admin/WebConsole/rest/index.php b/admin/WebConsole/rest/index.php
index 32aff3d3..46957ab6 100644
--- a/admin/WebConsole/rest/index.php
+++ b/admin/WebConsole/rest/index.php
@@ -37,7 +37,7 @@ $app = new \Slim\Slim(array(
'mode' => 'development',
'log.enabled' => true,
'log.level' => \Slim\Log::ERROR,
- 'debug' => false));
+ 'debug' => true));
$app->setName('opengnsys');
// Global variables.