zimonh/log-viewer-custom-log-path

为您的 Laravel 应用程序提供快速且易于使用的日志查看器

dev-main 2022-11-17 23:52 UTC

This package is auto-updated.

Last update: 2024-09-18 03:58:11 UTC


README

带一个额外配置参数的日志查看器分支
易于使用、快速且美观

功能 | 安装 | 配置 | 授权 | 故障排除 | 致谢

Packagist Packagist PHP from Packagist Laravel Version

log-viewer-light-dark

OPcodes的 日志查看器 是您 Laravel 应用的完美伴侣。

您不再需要阅读原始的 Laravel 日志文件来寻找您要查找的内容。

日志查看器可以帮助您快速清晰地查看单个日志条目,以便 搜索过滤 和快速理解您的 Laravel 日志。它是免费且易于安装的。

📺 观看4分钟快速视频 展示一些日志查看器的功能。

功能

  • 📂 查看storage/logs 目录中的所有 Laravel 日志,
  • 🔍 搜索 日志,
  • 🎚 按日志级别(错误、信息、调试等)进行过滤,
  • 🔗 可分享链接 到单个日志条目,
  • 🌑 暗黑模式
  • 💾 从UI下载和删除 日志文件,
  • ☑️ Horizon 日志支持,
  • 等等...

开始使用

要求

  • PHP 8.0+
  • Laravel 8+

安装

要使用 composer 安装此包,请运行

composer require zimonh/log-viewer-custom-log-path

使用

安装完成后,您可以直接在浏览器中访问 日志查看器

默认情况下,应用程序在以下位置可用:{APP_URL}/log-viewer

(例如:https://my-app.test/log-viewer

配置

配置文件

要发布 配置文件,请运行

php artisan vendor:publish --tag="log-viewer-config"

路由 & 中间件

您可以在 config/log-viewer.php 中轻松更改默认路由及其中间件。

请参见以下配置

    /*
    |--------------------------------------------------------------------------
    | Log Viewer Domain
    |--------------------------------------------------------------------------
    | You may change the domain where Log Viewer should be active.
    | If the domain is empty, all domains will be valid.
    |
    */

    'route_domain' => null,

    /*
    |--------------------------------------------------------------------------
    | Log Viewer Route
    |--------------------------------------------------------------------------
    | Log Viewer will be available under this URL.
    |
    */

    'route_path' => 'log-viewer',

    /*
    |--------------------------------------------------------------------------
    | Log Viewer route middleware.
    |--------------------------------------------------------------------------
    | The middleware should enable session and cookies support in order for the Log Viewer to work.
    | The 'web' middleware will be applied automatically if empty.
    |
    */

    'middleware' => ['web'],

授权

可以根据用户登录情况或正在操作的日志文件配置一些内容以实现不同的访问权限。

以下是权限及其设置方法。

授权日志查看器访问

您可以通过多种方式限制对日志查看器的访问。

通过 "auth" 回调

您可以通过向您的 AppServiceProvider 中的 LogViewer::auth() 方法提供自定义授权回调来限制对日志查看器的访问,如下所示

use Zimonh\LogViewer\Facades\LogViewer;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    LogViewer::auth(function ($request) {
        // return true to allow viewing the Log Viewer.
    });

    // Here's an example:
    LogViewer::auth(function ($request) {
        return $request->user()
            && in_array($request->user()->email, [
                // 'john@example.com',
            ]);
    });
}

通过 "viewLogViewer" 网关

限制对日志查看器访问的另一种简单方法是使用 Laravel 网关。只需在您的 App\Providers\AuthServiceProvider 类中定义一个 viewLogViewer 授权网关即可

use App\Models\User;
use Illuminate\Support\Facades\Gate;
 
/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();
 
    Gate::define('viewLogViewer', function (?User $user) {
        // return true if the user is allowed access to the Log Viewer
    });
}

通过中间件

您可以使用配置文件中的 auth 中间件轻松地将 认证 添加到查看日志的路由。

如果您的应用程序不使用默认的认证解决方案,您可以使用 auth.basic HTTP Basic 认证 中间件。

注意:默认情况下,auth.basic 中间件将假设您的用户数据库表中的电子邮件列是用户的“用户名”。

请参阅下面的 auth 中间件配置

    /*
    |--------------------------------------------------------------------------
    | Log Viewer route middleware.
    |--------------------------------------------------------------------------
    | The middleware should enable session and cookies support in order for the Log Viewer to work.
    | The 'web' middleware will be applied automatically if empty.
    |
    */

    'middleware' => ['web', 'auth'],

关于使用 Spatie 权限进行授权,请参阅此讨论 see this discussion

授权日志文件下载

您可以通过 Laravel Gates 限制下载日志文件的能力。只需在您的 App\Providers\AuthServiceProvider 类中定义一个 downloadLogFile 授权网关即可

use App\Models\User;
use Zimonh\LogViewer\LogFile;
use Illuminate\Support\Facades\Gate;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();
 
    Gate::define('downloadLogFile', function (?User $user, LogFile $file) {
        // return true if the user is allowed to download the specific log file.
    });
}

授权文件夹下载

您还可以通过定义一个 downloadLogFolder 授权网关来限制是否可以下载整个文件夹

use Zimonh\LogViewer\LogFolder;

//...

Gate::define('downloadLogFolder', function (?User $user, LogFolder $folder) {
    // return true if the user is allowed to download the whole folder.
});

注意:在下载之前也会检查单个文件的权限,以避免意外下载受保护的日志文件。

授权日志文件删除

您可以通过 Laravel Gates 限制删除日志文件的能力。只需在您的 App\Providers\AuthServiceProvider 类中定义一个 deleteLogFile 授权网关即可

use App\Models\User;
use Zimonh\LogViewer\LogFile;
use Illuminate\Support\Facades\Gate;

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();
 
    Gate::define('deleteLogFile', function (?User $user, LogFile $file) {
        // return true if the user is allowed to delete the specific log file.
    });
}

授权文件夹删除

您还可以通过定义一个 deleteLogFolder 授权网关来限制是否可以删除整个文件夹

use Zimonh\LogViewer\LogFolder;

//...

Gate::define('deleteLogFolder', function (?User $user, LogFolder $folder) {
    // return true if the user is allowed to delete the whole folder.
});

注意:在删除之前也会检查单个文件的权限,以避免意外删除受保护的日志文件。

故障排除

以下是一些常见问题和解决方案。

问题:“Livewire 未定义”或浏览器控制台中的其他错误

这通常是由于您的项目从子文件夹中提供,例如 example.com/your-laravel-project/log-viewer

默认情况下,Livewire 会尝试从域根目录加载其资源,例如 example.com/livewire/livewire.js,但如果它位于您的项目子文件夹之外,则需要设置不同的 asset_url。您可以在这里了解更多信息 read more about it here

幸运的是,修复方法很简单

  1. 发布 Livewire 配置
php artisan livewire:publish --config
  1. config/livewire.php 文件中将 asset_url 选项设置为您的应用子域名
    'asset_url' => '/your-laravel-project',

问题:日志未加载

目前,Log Viewer 只能处理类似以下格式的 Laravel 日志 Laravel logs

[2022-08-25 11:16:17] local.DEBUG: Example log entry for the level debug {"one":1,"two":"two","three":[1,2,3]}
Multiple lines are allowed
and will be picked up as contents
of the same log entry.

如果您的日志格式不同,您将必须等待我们推出对自定义日志格式的支持。否则,请调整您的日志格式为 Laravel 的默认格式。

屏幕截图

阅读有关 Log Viewer 功能的更多信息以及屏幕截图 release blog post

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请审查我们的安全策略以了解如何报告安全漏洞 our security policy

鸣谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 License File