Skip to content

Commit

Permalink
Log update API requests to a request.log file in a /logs subdirectory.
Browse files Browse the repository at this point in the history
  • Loading branch information
YahnisElsts committed Mar 5, 2013
1 parent ffd5265 commit 16bcc79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions includes/Wpup/UpdateServer.php
@@ -1,6 +1,7 @@
<?php
class Wpup_UpdateServer {
protected $packageDirectory;
protected $logDirectory;
protected $cache;
protected $serverUrl;
protected $startTime = 0;
Expand All @@ -21,6 +22,7 @@ public function __construct($serverUrl = null, $serverDirectory = null) {

$this->serverUrl = $serverUrl;
$this->packageDirectory = $serverDirectory . '/packages';
$this->logDirectory = $serverDirectory . '/logs';
$this->cache = new Wpup_FileCache($serverDirectory . '/cache');
}

Expand All @@ -36,6 +38,7 @@ public function handleRequest($query = null) {
}

$request = $this->initRequest($query);
$this->logRequest($request);
$this->checkAuthorization($request);
$this->dispatch($request);
exit;
Expand Down Expand Up @@ -190,6 +193,32 @@ protected function generateDownloadUrl(Wpup_Package $package) {
return $this->serverUrl . '?' . http_build_query($query, '', '&');
}

/**
* Log an API request.
*
* @param Wpup_Request $request
*/
protected function logRequest($request) {
$logFile = $this->logDirectory . '/request.log';
$handle = fopen($logFile, 'a');
if ( $handle && flock($handle, LOCK_EX) ) {
fwrite(
$handle,
sprintf(
"[%s] %s\t%s\t%s\n",
date('Y-m-d H:i:s'),
$request->action,
$request->slug,
http_build_query($request->query, '', '&')
)
);
flock($handle, LOCK_UN);
}
if ( $handle ) {
fclose($handle);
}
}

/**
* Output something as JSON.
*
Expand Down
2 changes: 2 additions & 0 deletions logs/.htaccess
@@ -0,0 +1,2 @@
Order deny,allow
Deny from all

0 comments on commit 16bcc79

Please sign in to comment.