saasscaleup/laravel-console-log 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

saasscaleup/laravel-console-log

Composer 安装命令:

composer require saasscaleup/laravel-console-log

包简介

Easily stream your Laravel application logs to the browser console.log in real-time using server-sent event (SSE)

README 文档

README

Main Window two

Easily stream your Laravel application logs to the browser console tab (console.log) in real-time using server-sent event (SSE)

This package lets backend developers easily print messages and values in terminal. This is the php version of console log for Laravel. Compatible with Laravel v5+.

Youtube · Twitter · Facebook · Buy Me a Coffee

Latest Stable Version Total Downloads License

✨ Features

  • Easily stream your Backend events from your Controllers \ Events \ Models \ Etc... to Frontend browser console tab(console.log')(data)).
  • Easily stream your Application Logs (storage/logs/laravel.log)to Frontend browser console tab (console.log')(data)).

banner

Requirements

  • PHP >= 7
  • Laravel >= 5

Installation

Install composer package (dev)

Via Composer - Not recommended for production environment

composer require --dev saasscaleup/laravel-console-log

For Laravel < 5.5

Add Service Provider to config/app.php in providers section

Saasscaleup\LCL\LCLServiceProvider::class,

Add Facade to config/app.php in aliases section

'LCL' => Saasscaleup\LCL\Facades\LCLFacade::class,

Publish package's config, migration and view files

Publish package's config, migration and view files by running below command:

php artisan vendor:publish --provider="Saasscaleup\LCL\LCLServiceProvider"

Run migration command

Run php artisan migrate to create stream_console_logs table.

php artisan migrate

Setup Laravel Console Log -> LCL

Add this in your main view/layout (usually layout/app.blade.php) file before :

@include('lcl::view')
<body>
...
@include('lcl::view')
</body>

Configuration

Configuration is done via environment variables or directly in the configuration file (config/lcl.php).

<?php

return [

    // enable or disable LCL
    'enabled' => env('LCL_ENABLED', true),

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

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

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

    // echo data loop in LCLController
    'interval' => env('LCL_INTERVAL', 1),

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

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

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

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

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

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

];

Usage

Syntax:

/**
* @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
 */
LCLFacade::notify($message, $type = 'info', $event = 'stream-console-log')

To show popup notifications on the screen, in your controllers/event classes, you can do:

use Saasscaleup\LCL\Facades\LCLFacade;

public function myFunction()
{

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

    // Some code here

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

    // Some code here

    LCLFacade::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');


    // or using your application
    \Log::info('Invoke stream log via application log 1');
    \Log::error('Invoke stream log via application log 2');
    \Log::debug('Invoke stream log via application log 3');

}

Customizing Notification Library

You can also, customize this by modifying code in resources/views/vendor/lcl/view.blade.php file.

Customizing LCL Events

By default, package uses stream-console-log event type for streaming response:

LCLFacade::notify($message, $type = 'info', $event = 'stream-console-log')

Notice $event = 'stream-console-log'. You can customize this, let's say you want to use UserPurchase as SSE event type:

use Saasscaleup\LCL\Facades\LCLFacade;

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

Then you need to handle this in your view yourself like this:

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

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

</script>

Inspired By

open-source

License

Please see the MIT for more information.

Support 🙏😃

If you Like the tutorial and you want to support my channel so I will keep releasing amzing content that will turn you to a desirable Developer with Amazing Cloud skills... I will realy appricite if you:

  1. Subscribe to our youtube
  2. Buy me A coffee ❤️

Thanks for your support :)

saasscaleup/laravel-console-log 适用场景与选型建议

saasscaleup/laravel-console-log 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 316 次下载、GitHub Stars 达 64, 最近一次更新时间为 2024 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「debug」 「event」 「notifications」 「server」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 saasscaleup/laravel-console-log 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 saasscaleup/laravel-console-log 我们能提供哪些服务?
定制开发 / 二次开发

基于 saasscaleup/laravel-console-log 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 316
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 64
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 64
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-17