Skip to content

Commit f2772d2

Browse files
author
Abdur Rahman
committed
initial commit
0 parents  commit f2772d2

File tree

9 files changed

+376
-0
lines changed

9 files changed

+376
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Abdur Rahman
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# WriteUp
2+
3+
[![Total Downloads](https://img.shields.io/packagist/dt/abdurrahmanriyad/writeup.svg?style=flat-square)](https://packagist.org/packages/abdurrahmanriyad/writeup)
4+
5+
A Laravel package to log necessary data in a request lifecycle.
6+
7+
## Installation
8+
9+
You can install the package via composer:
10+
11+
```bash
12+
composer require abdurrahmanriyad/writeup
13+
```
14+
15+
## Usage
16+
- Publish the assets
17+
``` php
18+
php artisan writeup:publish
19+
```
20+
- Find writeup config file in laravel config folder and <br>
21+
setup as configure as you need.
22+
23+
### Testing
24+
25+
``` bash
26+
composer test
27+
```
28+
29+
/**
30+
### Changelog
31+
32+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
33+
34+
## Contributing
35+
36+
Open issue > Solve > Pull Request > Merged
37+
38+
### Security
39+
40+
If you discover any security related issues, please email riyad.abdur@gmail.com instead of using the issue tracker.
41+
42+
## Credits
43+
44+
- [Abdur Rahman](https://github.com/abdurrahmanriyad)
45+
- All Contributors
46+
47+
## License
48+
49+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "abdurrahmanriyad/writeup",
3+
"description": "A Laravel package to log necessary data in a request lifecycle.",
4+
"keywords": [
5+
"abdurrahmanriyad",
6+
"writeup",
7+
"laravel",
8+
"debug",
9+
"laravel log",
10+
"log",
11+
"request log",
12+
"request response time",
13+
"response time",
14+
"write log"
15+
],
16+
"homepage": "https://github.com/abdurrahmanriyad/writeup",
17+
"license": "MIT",
18+
"type": "library",
19+
"authors": [
20+
{
21+
"name": "Abdur Rahman",
22+
"email": "riyad.abdur@gmail.com",
23+
"role": "Developer"
24+
}
25+
],
26+
"minimum-stability": "dev",
27+
"require": {
28+
"php": "^7.1"
29+
},
30+
"require-dev": {
31+
"phpunit/phpunit": "^8.0|^9.0"
32+
},
33+
"autoload": {
34+
"psr-4": {
35+
"Abdurrahmanriyad\\Writeup\\": "src"
36+
}
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Abdurrahmanriyad\\Writeup\\Tests\\": "tests"
41+
}
42+
},
43+
"scripts": {
44+
"test": "vendor/bin/phpunit",
45+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
46+
47+
},
48+
"config": {
49+
"sort-packages": true
50+
},
51+
"extra": {
52+
"laravel": {
53+
"providers": [
54+
"Abdurrahmanriyad\\Writeup\\WriteupServiceProvider"
55+
]
56+
}
57+
}
58+
}

config/config.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
return [
4+
'run_in_production' => env('WRITEUP_RUN_IN_PRODUCTION', true),
5+
'request_log' => [
6+
'enable' => env('WRITEUP_REQUEST_LOG', true),
7+
'header' => [
8+
'enable' => true,
9+
/**
10+
*** Following are some default headers. You may pass names which you want to log
11+
* host
12+
* connection
13+
* cache-control
14+
* upgrade-insecure-requests
15+
* user-agent
16+
* accept
17+
* sec-fetch-site
18+
* sec-fetch-mode
19+
* sec-fetch-user
20+
* sec-fetch-dest
21+
* accept-encoding
22+
* accept-language
23+
* cookie
24+
* cache-control
25+
*/
26+
'take' => ['host', 'connection', 'cache-control', 'user-agent', 'accept'],
27+
],
28+
'url' => true,
29+
'method' => true,
30+
'params' => true
31+
],
32+
'response_log' => [
33+
'enable' => env('WRITEUP_RESPONSE_LOG', true),
34+
'time' => true // Time required to execute a request
35+
],
36+
'query_log' => [
37+
'enable' => env('WRITEUP_QUERY_LOG', true),
38+
'sql' => true,
39+
'execution_time' => true,
40+
'connection' => true,
41+
'request_url' => true,
42+
'request_method' => true,
43+
]
44+
];

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

src/WriteupQueryMiddleware.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Abdurrahmanriyad\Writeup;
4+
5+
use Carbon\Carbon;
6+
use Closure;
7+
use Exception;
8+
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Facades\Log;
10+
11+
class WriteupQueryMiddleware
12+
{
13+
const DATE_TIME_FORMAT = 'Y/m/d h:m:s a';
14+
15+
public function handle($request, Closure $next)
16+
{
17+
try {
18+
$data = [];
19+
DB::listen(function ($query) use ($request, $data) {
20+
if (config('writeup.query_log.sql')) {
21+
$data['sql'] = $query->sql;
22+
}
23+
24+
if (config('writeup.query_log.execution_time')) {
25+
$data['execution_time'] = $query->time . ' millisecond(s)';
26+
}
27+
28+
if (config('writeup.query_log.connection')) {
29+
$data['connection'] = $query->connectionName;
30+
}
31+
32+
if (config('writeup.query_log.url')) {
33+
$data['url'] = $request->fullUrl();
34+
}
35+
36+
if (config('writeup.query_log.method')) {
37+
$data['method'] = $request->method();
38+
}
39+
40+
if (count($data)) {
41+
Log::info("Writeup Query", $data);
42+
}
43+
});
44+
} catch (Exception $exception) {
45+
Log::error("Writeup couldn't write a log. Please report to package owner");
46+
}
47+
48+
return $next($request);
49+
}
50+
}

src/WriteupRequestMiddleware.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Abdurrahmanriyad\Writeup;
4+
5+
use Closure;
6+
use Exception;
7+
use Illuminate\Support\Facades\Log;
8+
9+
class WriteupRequestMiddleware
10+
{
11+
const DATE_TIME_FORMAT = 'Y/m/d h:m:s a';
12+
13+
public function handle($request, Closure $next)
14+
{
15+
$data = [];
16+
17+
try {
18+
if (config('writeup.request_log.url')) {
19+
$data['url'] = $request->fullUrl();
20+
}
21+
22+
if (config('writeup.request_log.method')) {
23+
$data['method'] = $request->method();
24+
}
25+
26+
if (config('writeup.request_log.method')) {
27+
$data['header'] = $this->getSupportedHeaderFrom($request);
28+
}
29+
30+
if (config()) {
31+
$data['data'] = $request->all();
32+
}
33+
34+
if (count($data)) {
35+
Log::info("Writeup Request", $data);
36+
}
37+
} catch (Exception $e) {
38+
Log::error("Writeup couldn't write a log. Please report to package owner");
39+
}
40+
41+
return $next($request);
42+
}
43+
44+
private function getSupportedHeaderFrom($request)
45+
{
46+
$headers = collect($request->headers);
47+
return $headers->only(config('writeup.request_log.header.take'))->toArray();
48+
}
49+
}

src/WriteupResponseMiddleware.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Abdurrahmanriyad\Writeup;
4+
5+
use Carbon\Carbon;
6+
use Closure;
7+
use Exception;
8+
use Illuminate\Support\Facades\Log;
9+
10+
class WriteupResponseMiddleware
11+
{
12+
public function handle($request, Closure $next)
13+
{
14+
return $next($request);
15+
}
16+
public function terminate($request, $response)
17+
{
18+
try {
19+
$response_time = (microtime(true) - LARAVEL_START) * 1000;
20+
21+
if (config('writeup.response_log.time')) {
22+
Log::info('Writeup request response time : ' . $response_time . ' milliseconds');
23+
}
24+
} catch (Exception $exception) {
25+
Log::error("Writeup couldn't write a log. Please report to package owner");
26+
}
27+
}
28+
}

src/WriteupServiceProvider.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Abdurrahmanriyad\Writeup;
4+
5+
use Illuminate\Routing\Router;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class WriteupServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* @param Router $router
12+
*/
13+
public function boot(Router $router)
14+
{
15+
if ($this->app->runningInConsole()) {
16+
$this->publishes([
17+
__DIR__ . '/../config/config.php' => config_path('writeup.php'),
18+
], 'config');
19+
}
20+
21+
22+
if (config('writeup.run_in_production')) {
23+
if (config('writeup.request_log.enable')) {
24+
$router->pushMiddlewareToGroup('api', WriteupRequestMiddleware::class);
25+
$router->pushMiddlewareToGroup('web', WriteupRequestMiddleware::class);
26+
}
27+
28+
if (config('writeup.response_log.enable')) {
29+
$router->pushMiddlewareToGroup('api', WriteupResponseMiddleware::class);
30+
$router->pushMiddlewareToGroup('web', WriteupResponseMiddleware::class);
31+
}
32+
33+
if (config('writeup.query_log.enable')) {
34+
$router->pushMiddlewareToGroup('api', WriteupQueryMiddleware::class);
35+
$router->pushMiddlewareToGroup('web', WriteupQueryMiddleware::class);
36+
}
37+
}
38+
}
39+
40+
/**
41+
* Register the application services.
42+
*/
43+
public function register()
44+
{
45+
// Automatically apply the package configuration
46+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'writeup');
47+
}
48+
}

0 commit comments

Comments
 (0)