TelescopeServiceProvider.php
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace App\Providers;
use App\Models\SystemHttpLog;
use Arr;
use DB;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Gate;
use Laravel\Telescope\EntryType;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Laravel\Telescope\TelescopeApplicationServiceProvider;
use Str;
class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->hideSensitiveRequestDetails();
$this->filter();
}
/**
* Prevent sensitive request details from being logged by Telescope.
*/
protected function hideSensitiveRequestDetails(): void
{
Telescope::hideRequestHeaders(['cookie', 'x-csrf-token', 'x-xsrf-token']);
}
/**
* Register the Telescope gate.
*
* This gate determines who can access Telescope in non-local environments.
*/
protected function gate(): void
{
Gate::define('viewTelescope', static fn() => true);
}
/**
* @return void
*/
protected function filter(): void
{
Telescope::filter(static function (IncomingEntry $entry) {
if ($entry->type === EntryType::REQUEST) {
$content = $entry->content;
dispatch(static fn() => DB::table('system_http_logs')->insertOrIgnore([
'guard' => Str::of($content['uri'])->ltrim('/')->before('/')->ucfirst()->whenEmpty(fn() => Str::of('Web'))?->toString(),
'user_id' => Arr::get($content, 'user.id', 0),
'method' => $content['method'],
'uri' => Str::before($content['uri'], '?'),
'ip' => $content['ip_address'],
'request' => json_encode(['header' => $content['headers'], 'payload' => $content['payload']], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
'response' => json_encode(is_string($content['response']) ? ['content' => $content['response']] : $content['response'], JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
'consume' => $content['duration'],
'created_at' => Carbon::now()->toDateTimeString()
]))->afterResponse();
return true;
}
if ($entry->type === EntryType::MODEL && $entry->content['action'] === "created" && Str::contains($entry->content['model'], SystemHttpLog::class)) {
return false;
}
return true;
// return $entry->isReportableException() ||
// $entry->isFailedRequest() ||
// $entry->isFailedJob() ||
// $entry->isScheduledTask() ||
// $entry->hasMonitoredTag();
});
}
}