Skip to content

Commit ac3df15

Browse files
committed
add response time in log
1 parent be2b216 commit ac3df15

File tree

4 files changed

+60
-15
lines changed

4 files changed

+60
-15
lines changed

src/Classes/Logable.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,44 @@
88

99
class Logable extends Log
1010
{
11+
private $arrived;
12+
private $left;
13+
private $duration;
14+
private string $channel;
1115

16+
17+
public function __construct()
18+
{
19+
$this->channel = 'logable';
20+
}
21+
22+
public function log($message, $level = 'info')
23+
{
24+
self::channel($this->channel)->$level($message);
25+
}
26+
27+
public function setArrivingTime($time)
28+
{
29+
return $this->arrived = $time;
30+
}
31+
32+
public function setLeavingTime($time)
33+
{
34+
return $this->left = $time;
35+
}
36+
37+
public function getArrivedTime()
38+
{
39+
return $this->arrived;
40+
}
41+
42+
public function getLeftTime()
43+
{
44+
return $this->left;
45+
}
46+
47+
public function getTotalDuration()
48+
{
49+
return $this->duration = $this->left - $this->arrived;
50+
}
1251
}

src/Console/Commands/MonitorLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private function sanitizeMessage(array $messages)
187187
continue;
188188
}
189189

190-
$validData[] = $message;
190+
$validData[] = wordwrap($message, config('logable.wordwrap', 50), "\n", true);
191191
}
192192

193193
return $validData;
@@ -201,7 +201,7 @@ private function sanitizeMessage(array $messages)
201201
* @param string $tableStyle
202202
* @param array $columnStyles
203203
*/
204-
public function table($headers, $rows, $tableStyle = 'symfony-style-guide', array $columnStyles = [])
204+
public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
205205
{
206206
$table = new Table($this->output);
207207

src/LogableServiceProvider.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sagar290\Logable;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Sagar290\Logable\Classes\Logable;
67
use Sagar290\Logable\Console\Commands\ClearLog;
78
use Sagar290\Logable\Console\Commands\MonitorLog;
89

@@ -15,9 +16,9 @@ class LogableServiceProvider extends ServiceProvider
1516
*/
1617
public function register()
1718
{
18-
19-
20-
19+
app()->singleton('logable', function () {
20+
return new Logable;
21+
});
2122
}
2223

2324
/**

src/Middleware/RouteLogMiddleware.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@
22

33
namespace Sagar290\Logable\Middleware;
44

5+
use Carbon\Carbon;
56
use Closure;
67
use Illuminate\Support\Facades\Artisan;
78
use Illuminate\Support\Facades\Log;
89

910
class RouteLogMiddleware
1011
{
11-
/**
12-
* Handle an incoming request.
13-
*
14-
* @param \Illuminate\Http\Request $request
15-
* @param \Closure $next
16-
* @return mixed
17-
*/
12+
public $timer;
13+
1814
public function handle($request, Closure $next)
1915
{
16+
app('logable')->setArrivingTime(microtime(true));
17+
18+
return $next($request);
19+
}
20+
21+
public function terminate($request, $response)
22+
{
23+
$logable = app('logable');
2024

21-
//log the request
22-
Log::channel('logable')->info(json_encode([
25+
$logable->setLeavingTime(microtime(true));
26+
27+
$logable->log(json_encode([
2328
'path' => $request->path(),
2429
'method' => $request->method(),
2530
'ip' => $request->ip(),
2631
'user_agent' => $request->userAgent(),
2732
'body' => $request->all(),
33+
'response_time' => Carbon::parse($logable->getTotalDuration())->format('Uv') . ' ms',
2834
]));
2935

30-
return $next($request);
3136
}
3237
}

0 commit comments

Comments
 (0)