Skip to content

Commit 1bfb0df

Browse files
committed
chore(package): setup
1 parent fc0ce6c commit 1bfb0df

File tree

10 files changed

+338
-2
lines changed

10 files changed

+338
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.

.github/pull_request_template.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
### Description
2+
3+
Explain what are you submitting.
4+
5+
### Fixes
6+
7+
Enter the issue you are fixing (e.g. This fixes #1.).
8+
9+
10+
11+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea

README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,69 @@
1-
# laravel-api-logs
2-
Simple package to log requests made to your API.
1+
# Laravel API Requests Logger
2+
3+
A lightweight Laravel package for logging requests made to your API.
4+
5+
[![Latest version](https://img.shields.io/github/release/CodeTechAgency/laravel-api-logs?style=flat-square)](https://github.com/CodeTechAgency/laravel-api-logs/releases)
6+
[![GitHub license](https://img.shields.io/github/license/CodeTechAgency/laravel-api-logs?style=flat-square)](https://github.com/CodeTechAgency/laravel-api-logs/blob/master/LICENSE)
7+
8+
## Installation
9+
10+
Add the package to your Laravel application using composer:
11+
12+
```
13+
composer require codetech/laravel-api-logs
14+
```
15+
16+
17+
### Service Provider
18+
19+
The service provider will be automatically registered during the installation process. However, you can manually register it by adding it to the list of providers located in your `config/app.php` file:
20+
21+
```
22+
'providers' => [
23+
...
24+
Codetech\ApiLogs\Providers\ApiLogServiceProvider::class,
25+
26+
],
27+
```
28+
29+
30+
### Migrations
31+
32+
Publish the migration file:
33+
34+
```
35+
php artisan vendor:publish --provider=CodeTech\\ApiLogs\\Providers\\ApiLogServiceProvider --tag=migrations
36+
```
37+
38+
Run the migration:
39+
```
40+
php artisan migrate
41+
```
42+
43+
## Usage
44+
45+
To start logging requests made to your API, you simply add the middleware to the API's route middleware group, located in your `app/Http/Kernel.php`:
46+
47+
```
48+
protected $middlewareGroups = [
49+
...
50+
51+
'api' => [
52+
...
53+
LogApiRequest::class,
54+
],
55+
];
56+
```
57+
58+
59+
---
60+
61+
62+
## License
63+
64+
**codetech/laravel-api-logs** is open-sourced software licensed under the [MIT license](https://github.com/CodeTechAgency/laravel-api-logs/blob/master/LICENSE).
65+
66+
67+
## About CodeTech
68+
69+
[CodeTech](https://www.codetech.pt) is a web development agency based in Matosinhos, Portugal. Oh, and we LOVE Laravel!

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "codetech/laravel-api-logs",
3+
"description": "A lightweight Laravel package for logging requests made to your API.",
4+
"keywords": [
5+
"codetech",
6+
"laravel-api-logs",
7+
"api",
8+
"logs",
9+
"laravel",
10+
"laravel-package"
11+
],
12+
"authors": [
13+
{
14+
"name": "José Osório",
15+
"email": "jfrosorio@gmail.com",
16+
"role": "Developer"
17+
}
18+
],
19+
"license": "MIT",
20+
"require": {
21+
"php": "^7.2|^8.0",
22+
"illuminate/database": "^7.0|^8.0|^9.0|^10.0",
23+
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"CodeTech\\ApiLogs\\": "src/"
28+
}
29+
},
30+
"minimum-stability": "dev",
31+
"prefer-stable": true,
32+
"extra": {
33+
"laravel": {
34+
"providers": [
35+
"CodeTech\\ApiLogs\\Providers\\ApiLogServiceProvider"
36+
]
37+
}
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateApiLogsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('api_logs', function (Blueprint $table) {
17+
$table->id();
18+
$table->float('duration');
19+
$table->string('url');
20+
$table->string('method');
21+
$table->ipAddress('ip');
22+
$table->json('request_data');
23+
$table->json('response_data')->nullable();
24+
$table->foreignId('user_id')->nullable()->constrained()->cascadeOnDelete()->cascadeOnUpdate();
25+
$table->timestamps();
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists('api_logs');
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace CodeTech\ApiLogs\Http\Middleware;
4+
5+
use Closure;
6+
use CodeTech\ApiLogs\Models\ApiLog;
7+
8+
class LogApiRequest
9+
{
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param $request
14+
* @param Closure $next
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
$request->start = microtime(true);
20+
21+
return $next($request);
22+
}
23+
24+
/**
25+
* Handle tasks after the response has been sent to the browser.
26+
*
27+
* @param $request
28+
* @param $response
29+
*/
30+
public function terminate($request, $response)
31+
{
32+
$request->end = microtime(true);
33+
34+
$data = [
35+
'duration' => $request->end - $request->start,
36+
'url' => $request->fullUrl(),
37+
'method' => $request->getMethod(),
38+
'ip' => $request->getClientIp(),
39+
'request_data' => $request->all(),
40+
'response_data' => json_decode($response->getContent()),
41+
'user_id' => auth()->check() ? auth()->id() : null,
42+
];
43+
44+
ApiLog::create($data);
45+
}
46+
}

src/Models/ApiLog.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace CodeTech\ApiLogs\Models;
4+
5+
use App\Models\User;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class ApiLog extends Model
9+
{
10+
/**
11+
* @inheritdoc
12+
*/
13+
protected $fillable = [
14+
'duration',
15+
'url',
16+
'method',
17+
'ip',
18+
'request_data',
19+
'response_data',
20+
'user_id',
21+
];
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
protected $casts = [
27+
'request_data' => 'json',
28+
'response_data' => 'json',
29+
'created_at' => 'datetime:d/m/Y H:i',
30+
'updated_at' => 'datetime:d/m/Y H:i',
31+
];
32+
33+
/**
34+
* Get the user that owns this session log.
35+
*
36+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
37+
*/
38+
public function user()
39+
{
40+
return $this->belongsTo(User::class);
41+
}
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace CodeTech\ApiLogs\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ApiLogServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap any application services.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
26+
$this->setPublishableFiles();
27+
}
28+
29+
/**
30+
* Sets the publishable files.
31+
*/
32+
private function setPublishableFiles()
33+
{
34+
$databasePath = sprintf('migrations/%s_create_api_logs_table.php', date('Y_m_d_His', time()));
35+
36+
$this->publishes([
37+
__DIR__.'/../../database/migrations/create_api_logs_table.php.stub' => database_path($databasePath)
38+
], 'migrations');
39+
}
40+
}

0 commit comments

Comments
 (0)