bert-w / laravel-log-viewer
一个快速查看任何日志文件的 Laravel 日志查看器。
v0.4.0
2023-07-03 10:38 UTC
Requires
- php: ^7.4|^8.0
- illuminate/filesystem: *
- illuminate/http: *
- illuminate/pagination: *
This package is auto-updated.
Last update: 2024-09-16 23:25:32 UTC
README
一个易于添加和配置的快速 Laravel 日志文件查看器。
特性
- 兼容 Laravel 8 + 9 + 10
- 轻松读取大日志文件,无需达到内存限制
- 可以包含在你的 Blade 模板中,或者使用预定义布局
- Bootstrap 3 + 4 + 5 模板
- Bootstrap 5 包含 暗黑模式 设置
安装
- 安装包
composer require bert-w/laravel-log-viewer
- (这会自动完成,除非你绕过了 包发现) 将服务提供者添加到你的
config/app.php
'providers' => [ // ... BertW\LaravelLogViewer\LogViewerServiceProvider::class, ]
服务提供者设置了视图、路由、配置和认证。同时,\BertW\LaravelLogViewer\LogViewer::class
作为一个单例绑定到服务容器中。这允许你在任何地方注入日志查看器(例如在自定义控制器中)使用 $logViewer = app(\BertW\LaravelLogViewer\LogViewer::class)
。3. a)(可选)发布配置文件
php artisan vendor:publish --provider=BertW\LaravelLogViewer\LogViewerServiceProvider
- b) 或者手动将其复制到
config/logviewer.php
<?php return [ /* | The base URI for the log viewer. */ 'url' => '/logviewer', /* | The route name prefix to use for the logviewer route names. */ 'route_name_prefix' => 'logviewer.', /* | Display name for log files when they are listed in the interface, which is one of: | 'short' (filename only) or 'full' (absolute path). */ 'log_display_name' => 'short', /* | The title for the logviewer page. If `null`, no title is shown. */ 'title' => config('app.name') . ' Log Viewer', /* | The storage path that contains the logs to be displayed in the log viewer. */ 'storage_path' => storage_path('logs'), /* | The amount of lines from a log file that are read per page. The amount of logs that | are shown per page depends on this value, and it may differ depending on the | length of a single log record (Default: 3200). */ 'lines_per_page' => 3200, /* | The max length in bytes that a single line may have. Content that exceeds this limit will | be truncated from view. Note: disabling this feature with `null` may cause memory issues | with big log files that exceed this max line length (Default: 16000). */ 'max_line_length' => 16000, /* | The threshold in bytes for a log file to be marked as "big". This allows | the frontend to visually indicate that the log file is big (Default: 64MB). */ 'big_file_threshold' => 2**26, /* | The sorting order of the log files how they appear in the interface. */ 'sort_by' => ['modified_at', 'desc'], /* | Preselect the first log file based on the given ordering. | If `null`, no log file will be opened by default. */ 'preselect' => ['modified_at', 'desc'], ];
- 默认情况下,你的日志查看器应该在
/logviewer
可用。对于自定义配置,请参阅下面的 自定义 部分。
授权
为日志查看器设置授权很简单:在任意已加载的服务提供者(如 AppServiceProvider
)中,在 register()
方法中添加以下行
\BertW\LaravelLogViewer\LogViewer::auth(function(\Illuminate\Http\Request $request) { return $request->user()->role === 'admin'; });
默认情况下,如果没有提供自定义授权回调,日志查看器仅在 local
环境中可用。
自定义
将日志查看器包含在你的模板中可以通过修改视图文件 resources/views/vendor/logviewer/index.blade.php
(在发布命令后可用)来完成,它具有以下默认设置
@extends('logviewer::bootstrap-5.layout') @section('content') <div class="container"> @include('logviewer::bootstrap-5.index') </div> @endsection
标准布局包含从 https://cdn.jsdelivr.net.cn
链接的预包含 Bootstrap CSS/JS 文件。