saasscaleup/laravel-stream-log

使用服务器端事件(SSE)轻松将Laravel应用程序日志实时流到前端

1.0.6 2023-12-07 08:16 UTC

This package is auto-updated.

Last update: 2024-09-25 20:00:27 UTC


README

Main Window two

使用服务器端事件(SSE)轻松将Laravel应用程序日志实时流到前端

Youtube · Twitter · Facebook · Buy Me a Coffee

Latest Stable Version Total Downloads License

✨ 特点

  • 轻松将后端事件从控制器、事件、模型等流出来。
  • 轻松流日志(storage/logs/laravel.log)。
  • 将后端日志和事件打印到前端浏览器 console.log(data)

banner

要求

  • PHP >= 7
  • Laravel >= 5

安装

安装composer包(开发环境)

通过Composer - 不推荐用于生产环境

$ composer require --dev saasscaleup/laravel-stream-log

对于Laravel < 5.5

将服务提供者添加到 config/app.php 中的 providers 部分

Saasscaleup\LSL\LSLServiceProvider::class,

将外观添加到 config/app.php 中的 aliases 部分

'LSL' => Saasscaleup\LSL\Facades\LSLFacade::class,

发布包的配置、迁移和视图文件

通过运行以下命令发布包的配置、迁移和视图文件

$ php artisan vendor:publish --provider="Saasscaleup\LSL\LSLServiceProvider"

运行迁移命令

运行 php artisan migrate 以创建 stream_logs 表。

$ php artisan migrate

设置Laravel Stream Log -> LSL

在主视图/布局(通常是 layout/app.blade.php)文件中添加此内容

@include('lsl::view')

配置

配置通过环境变量或在配置文件(config/lsl.php)中直接完成。

<?php

return [

    // enable or disable LSL
    'enabled' => env('LSL_ENABLED', true),

    // enable or disable laravel log listener 
    'log_enabled' => env('LSL_LOG_ENABLED', true),

    // log listener  for specific log type
    'log_type' => env('LSL_LOG_TYPE', 'info,error,warning,alert,critical,debug'), // Without space

    // log listener for specific word inside log messages
    'log_specific' => env('LSL_LOG_SPECIFIC', ''), // 'test' or 'foo' or 'bar' or leave empty '' to disable this feature

    // echo data loop in LSLController
    'interval' => env('LSL_INTERVAL', 1),

    // append logged user id in LSL response
    'append_user_id' => env('LSL_APPEND_USER_ID', true),

    // keep events log in database
    'keep_events_logs' => env('LSL_KEEP_EVENTS_LOGS', false),

    // Frontend pull invoke interval
    'server_event_retry' => env('LSL_SERVER_EVENT_RETRY', '2000'),

    // every 10 minutes cache expired, delete logs on next request
    'delete_log_interval' => env('LSL_DELETE_LOG_INTERVAL', 600), 

    /******* Frontend *******/

    // eanlbed console log on browser
    'js_console_log_enabled' => env('LSL_JS_CONSOLE_LOG_ENABLED', true),

     // js notification toast library
    'js_notification_library' => env('LSL_JS_NOTIFICATION_LIBRARY', 'noty'), // 'izitoast' or 'noty'

    // notification settings
    'js_position' => 'bottomRight', // topLeft, topCenter, topRight, center, bottomLeft, bottomCenter, bottomRight
    'js_timeout' => 5000, // false, 1000, 3000, 3500, etc. Delay for closing event in milliseconds (ms). Set 'false' for sticky notifications.
];

用法

语法

/**
* @param string $message : notification message
* @param string $type : alert, success, error, warning, info, debug, critical, etc...
* @param string $event : Type of event such as "EmailSent", "UserLoggedIn", etc
 */
LSLFacade::notify($message, $type = 'info', $event = 'stream')

要在屏幕上显示弹出通知,在控制器/事件类中,您可以这样做

use Saasscaleup\LSL\Facades\LSLFacade;

public function myFunction()
{

    LSLFacade::notify('Invoke stream log via Facade 1');

    // Some code here

    LSLFacade::notify('Invoke stream log via Facade 2');

    // Some code here

    LSLFacade::notify('Invoke stream log via Facade 3');
    
    // or via helper
    stream_log('Invoke stream log via helper 1');
    stream_log('Invoke stream log via helper 2');     
    stream_log('Invoke stream log via helper 3');
}

自定义通知库

默认情况下,包使用 noty 来显示通知。

您可以通过更新配置文件 config/lsl.php 切换到 izitoast

 // js notification toast library
'js_notification_library' => env('LSL_JS_NOTIFICATION_LIBRARY', 'izitoast'), // 'izitoast' or 'noty'

您还可以通过修改 resources/views/vendor/lsl/view.blade.php 文件中的代码来自定义它。

自定义LSL事件

默认情况下,包使用 stream 事件类型进行流式传输

LSLFacade::notify($message, $type = 'info', $event = 'message')

注意 $event = 'stream'。您可以自定义此,例如,如果您想使用 UserPurchase 作为SSE事件类型

use Saasscaleup\LSL\Facades\LSLFacade;

public function myMethod()
{
    SSEFacade::notify('User purchase plan - step 1', 'info', 'UserPurchase');
    
    // or via helper
    stream_log('User purchase plan - step 1', 'info', 'UserPurchase');
}

然后您需要在视图中自己处理它,如下所示

<script>
var es = new EventSource("{{route('lsl-stream-log')}}");

es.addEventListener("UserPurchase", function (e) {
    var data = JSON.parse(e.data);
    alert(data.message);
}, false);

</script>

许可证

有关更多信息,请参阅 MIT

支持 🙏😃

如果您喜欢这个教程并想支持我的频道,以便我继续发布精彩的内容,让您成为具有出色云技能的理想开发者... 如果您这样做,我将不胜感激

  1. 订阅我们的 youtube
  2. 买我一杯 咖啡 ❤️

感谢您的支持 :)