如何更好地使用 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;
    });
}

查看效果
telescope

7. laravel-s环境下使用telescope

来自laravel-s官网的解决方法

本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 3年前 自动加精
windawake
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 11

不错

3年前 评论
windawake (楼主) 3年前

虽然没用过,但是看着好厉害的样子,先关注了

3年前 评论
萧晔离

收藏一下!

3年前 评论

比较好奇前后端分离的项目, 后端是 laravel 写接口, 此时用 telescope 鉴权咋弄

3年前 评论
windawake (楼主) 3年前
萧晔离

想问下,怎么样过滤掉OPTIONS请求呢

3年前 评论
windawake (楼主) 3年前
萧晔离 (作者) 3年前

楼主,想问下想自定义telescope 表,不想它自动创建,需要 怎么配置

2年前 评论

ignore_paths 好像不起作用,代码包有bug 需要把源码这段换成上面这样

file

6个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!