如何更好地使用 telescope
laravel官方telescope是一个强大的开发调试工具。由于对工具不熟悉,没法发挥强大工具的强项,所以我花了点时间研究telescope后,得出来一些使用技巧
1. 安装指导
参考社区文档 telescope
2. 线上环境只写入文件日志
// app/Providers/TelescopeServiceProvider.php
public function register()
{
// Telescope::night();
$this->hideSensitiveRequestDetails();
Telescope::filter(function (IncomingEntry $entry) {
if (!$this->app->environment('production')) {
return true;
}
Log::info('telescope', $entry->toArray());
return false;
});
}
3. telescope_entries记录数太多了,只想保存必要的
### 修改laravel .env
TELESCOPE_LOG_WATCHER=false
TELESCOPE_CACHE_WATCHER=false
TELESCOPE_REDIS_WATCHER=false
TELESCOPE_RESPONSE_SIZE_LIMIT=16
TELESCOPE_ENABLED=true
4. 文件日志适配elk抓取
创建一个文件 App\Helpers\Logging\DailyFormatter.php
<?php
namespace App\Helpers\Logging;
class DailyFormatter{
/**
* 自定义给定的日志实例。
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->getFormatter()->allowInlineLineBreaks(false);
}
}
}
从config/logging.php复制出daily渠道,并且加上tap中间件DailyFormatter
// config/logging.php 里面有很多Log Channels例如"single", "daily", "slack"等
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
'telescope' => [
'driver' => 'daily',
'path' => storage_path('logs/telescope/laravel.log'),
'level' => 'debug',
'tap' => [App\Helpers\Logging\DailyFormatter::class],
'days' => 14,
],
这时要修改以上第二项优化的文件TelescopeServiceProvider.php
// app/Providers/TelescopeServiceProvider.php
public function register()
{
// Telescope::night();
$this->hideSensitiveRequestDetails();
Telescope::filter(function (IncomingEntry $entry) {
if (!$this->app->environment('production')) {
return true;
}
Log::channel('telescope')->info($entry->type, $entry->toArray());
return false;
});
}
5. 不想记录高频率的基础数据接口,例如国家语言,map字段等
// config/telescope.php 增加需要忽略的uri
'ignore_paths' => [
'nova-api*',
'v1/language',
'v1/country',
'v1/fieldmap',
'v1/department',
'v1/area/*'
],
6. 快速搜索出post和put请求
// app/Providers/TelescopeServiceProvider.php
public function register()
{
// Telescope::night();
$this->hideSensitiveRequestDetails();
Telescope::tag(function (IncomingEntry $entry) {
if ($entry->type === 'request') {
return ['method:'.$entry->content['method']];
}
return [];
});
Telescope::filter(function (IncomingEntry $entry) {
if (!$this->app->environment('production')) {
return true;
}
$logAble = true;
if($entry->type == 'query') {
$sql = $entry->content['sql'];
if(strpos($sql,'telescope_entries') || strpos($sql,'telescope_entries_tags')) {
$logAble = false;
}
}
if($logAble) Log::channel('telescope')->info($entry->type, $entry->toArray());
return false;
});
}
查看效果
7. laravel-s环境下使用telescope
来自laravel-s官网的解决方法
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 4年前 自动加精
推荐文章: