Skip to content

Commit

Permalink
Merge pull request #36 from chrisbjr/awkwardusername-patch-1
Browse files Browse the repository at this point in the history
logging option per method
  • Loading branch information
awkwardusername committed Apr 8, 2015
2 parents ba5367e + 72fac17 commit 5862973
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,29 @@ class BooksController extends ApiGuardController

The above example will limit the request rate to the `show` method to 1000 requests per day.

Note: The `increment` option can be any value that is accepted by the `strtotime()` method.
Note: The `increment` option can be any value that is accepted by the `strtotime()` method.

### Logging at method level

You can set logging at method level by using the `logged` option.

```php
<?php

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;

class BooksController extends ApiGuardController
{

protected $apiMethods = [
'show' => [
'logged' => true
]
];

...

}
```

By default for all methods in api-guard, the option `logged` is set to true. Set it to `false` to exclude that method for logging.
34 changes: 24 additions & 10 deletions src/Http/Controllers/ApiGuardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,32 @@ public function __construct()
}

// End of cheking limits
if (Config::get('apiguard.logging', true) && $keyAuthentication == true) {
// Log this API request
$apiLog = App::make(Config::get('apiguard.apiLogModel', 'Chrisbjr\ApiGuard\Models\ApiLog'));
$apiLog->api_key_id = $this->apiKey->id;
$apiLog->route = Route::currentRouteAction();
$apiLog->method = $request->getMethod();
$apiLog->params = http_build_query(Input::all());
$apiLog->ip_address = $request->getClientIp();
$apiLog->save();
if (Config::get('api-guard::logging', true)) {
// Default to log requests from this action
$logged = true;

if (isset($apiMethods[$method]['logged']) && $apiMethods[$method]['logged'] === false) {
$logged = false;
}

if ($logged) {
// Log this API request
$apiLog = App::make(Config::get('api-guard::apiLogModel', 'Chrisbjr\ApiGuard\Models\ApiLog'));

if (isset($this->apiKey)) {
$apiLog->api_key_id = $this->apiKey->id;
}

$apiLog->route = Route::currentRouteAction();
$apiLog->method = $request->getMethod();
$apiLog->params = http_build_query(Input::all());
$apiLog->ip_address = $request->getClientIp();
$apiLog->save();

}
}

}, ['apiMethods' => $this->apiMethods]);
}

}
}

0 comments on commit 5862973

Please sign in to comment.