├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── lcl-demo.gif ├── lcl-saasscaleup.png └── src ├── .github └── FUNDING.yml ├── Config └── lcl.php ├── Facades └── LCLFacade.php ├── Helpers └── helper.php ├── Http ├── Controllers │ └── LCLController.php └── routes.php ├── LCL.php ├── LCLServiceProvider.php ├── Migrations └── 2023_12_05_99999_create_stream_console_logs_table.php ├── Models └── StreamConsoleLog.php └── Views └── view.blade.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: saasscaleup 4 | ko_fi: scaleupsaas 5 | custom: https://buymeacoffee.com/scaleupsaas 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Scale-Up SaaS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Main Window two](https://github.com/saasscaleup/laravel-console-log/blob/master/lcl-saasscaleup.png?raw=true) 2 | 3 |

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

4 | 5 |

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

7 |

8 | Youtube 9 | · 10 | Twitter 11 | · 12 | Facebook 13 | · 14 | Buy Me a Coffee 15 |

16 | 17 |

18 | 19 | Latest Stable Version 20 | 21 | 22 | 23 | Total Downloads 24 | 25 | 26 | 27 | License 28 | 29 |

30 | 31 | ## ✨ Features 32 | 33 | - **Easily stream your Backend events from your Controllers \ Events \ Models \ Etc... to Frontend browser console tab(`console.log')(data)`).** 34 | - **Easily stream your Application Logs (`storage/logs/laravel.log`)to Frontend browser console tab (`console.log')(data)`).** 35 | 36 |
37 | 38 | ![banner](https://github.com/saasscaleup/laravel-console-log/blob/master/lcl-demo.gif?raw=true) 39 |
40 | 41 | 42 | ## Requirements 43 | 44 | - PHP >= 7 45 | - Laravel >= 5 46 | 47 | ## Installation 48 | 49 | ### Install composer package (dev) 50 | 51 | Via Composer - Not recommended for production environment 52 | 53 | ``` bash 54 | composer require --dev saasscaleup/laravel-console-log 55 | ``` 56 | 57 | #### For Laravel < 5.5 58 | 59 | Add Service Provider to `config/app.php` in `providers` section 60 | ```php 61 | Saasscaleup\LCL\LCLServiceProvider::class, 62 | ``` 63 | 64 | Add Facade to `config/app.php` in `aliases` section 65 | ```php 66 | 'LCL' => Saasscaleup\LCL\Facades\LCLFacade::class, 67 | ``` 68 | 69 | 70 | --- 71 | 72 | ### Publish package's config, migration and view files 73 | 74 | 75 | Publish package's config, migration and view files by running below command: 76 | 77 | ```bash 78 | php artisan vendor:publish --provider="Saasscaleup\LCL\LCLServiceProvider" 79 | ``` 80 | 81 | ### Run migration command 82 | 83 | Run `php artisan migrate` to create `stream_console_logs` table. 84 | 85 | ```bash 86 | php artisan migrate 87 | ``` 88 | 89 | ## Setup Laravel Console Log -> LCL 90 | 91 | Add this in your main view/layout (usually `layout/app.blade.php`) file before : 92 | 93 | ```php 94 | @include('lcl::view') 95 | ``` 96 | 97 | ```php 98 | 99 | ... 100 | @include('lcl::view') 101 | 102 | ``` 103 | 104 | ## Configuration 105 | 106 | Configuration is done via environment variables or directly in the configuration file (`config/lcl.php`). 107 | 108 | ``` 109 | env('LCL_ENABLED', true), 115 | 116 | // enable or disable laravel log listener 117 | 'log_enabled' => env('LCL_LOG_ENABLED', true), 118 | 119 | // log listener for specific log type 120 | 'log_type' => env('LCL_LOG_TYPE', 'info,error,warning,alert,critical,debug'), // Without space 121 | 122 | // log listener for specific word inside log messages 123 | 'log_specific' => env('LCL_LOG_SPECIFIC', ''), // 'test' or 'foo' or 'bar' or leave empty '' to disable this feature 124 | 125 | // echo data loop in LCLController 126 | 'interval' => env('LCL_INTERVAL', 1), 127 | 128 | // append logged user id in LCL response 129 | 'append_user_id' => env('LCL_APPEND_USER_ID', true), 130 | 131 | // keep events log in database 132 | 'keep_events_logs' => env('LCL_KEEP_EVENTS_LOGS', false), 133 | 134 | // Frontend pull invoke interval 135 | 'server_event_retry' => env('LCL_SERVER_EVENT_RETRY', '2000'), 136 | 137 | // every 10 minutes cache expired, delete logs on next request 138 | 'delete_log_interval' => env('LCL_DELETE_LOG_INTERVAL', 600), 139 | 140 | /******* Frontend *******/ 141 | 142 | // eanlbed console log on browser 143 | 'js_console_log_enabled' => env('LCL_JS_CONSOLE_LOG_ENABLED', true), 144 | 145 | ]; 146 | ``` 147 | 148 | ## Usage 149 | 150 | Syntax: 151 | 152 | ```php 153 | /** 154 | * @param string $message : notification message 155 | * @param string $type : alert, success, error, warning, info, debug, critical, etc... 156 | * @param string $event : Type of event such as "EmailSent", "UserLoggedIn", etc 157 | */ 158 | LCLFacade::notify($message, $type = 'info', $event = 'stream-console-log') 159 | ``` 160 | 161 | To show popup notifications on the screen, in your controllers/event classes, you can do: 162 | 163 | ```php 164 | use Saasscaleup\LCL\Facades\LCLFacade; 165 | 166 | public function myFunction() 167 | { 168 | 169 | LCLFacade::notify('Invoke stream log via Facade 1'); 170 | 171 | // Some code here 172 | 173 | LCLFacade::notify('Invoke stream log via Facade 2'); 174 | 175 | // Some code here 176 | 177 | LCLFacade::notify('Invoke stream log via Facade 3'); 178 | 179 | // or via helper 180 | stream_log('Invoke stream log via helper 1'); 181 | stream_log('Invoke stream log via helper 2'); 182 | stream_log('Invoke stream log via helper 3'); 183 | 184 | 185 | // or using your application 186 | \Log::info('Invoke stream log via application log 1'); 187 | \Log::error('Invoke stream log via application log 2'); 188 | \Log::debug('Invoke stream log via application log 3'); 189 | 190 | } 191 | ``` 192 | 193 | 194 | 195 | ## Customizing Notification Library 196 | 197 | You can also, customize this by modifying code in `resources/views/vendor/lcl/view.blade.php` file. 198 | 199 | ## Customizing LCL Events 200 | 201 | By default, package uses `stream-console-log` event type for streaming response: 202 | 203 | 204 | ```php 205 | LCLFacade::notify($message, $type = 'info', $event = 'stream-console-log') 206 | ``` 207 | 208 | Notice `$event = 'stream-console-log'`. You can customize this, let's say you want to use `UserPurchase` as SSE event type: 209 | 210 | ```php 211 | use Saasscaleup\LCL\Facades\LCLFacade; 212 | 213 | public function myMethod() 214 | { 215 | LCLFacade::notify('User purchase plan - step 1', 'info', 'UserPurchase'); 216 | 217 | // or via helper 218 | stream_console_log('User purchase plan - step 1', 'info', 'UserPurchase'); 219 | } 220 | ``` 221 | 222 | Then you need to handle this in your view yourself like this: 223 | 224 | ```javascript 225 | 234 | ``` 235 | 236 | ## Inspired By 237 | 238 | [open-source](https://github.com/saasscaleup/laravel-stream-log) 239 | 240 | ## License 241 | 242 | Please see the [MIT](license.md) for more information. 243 | 244 | 245 | ## Support 🙏😃 246 | 247 | 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: 248 | 249 | 1. Subscribe to our [youtube](http://www.youtube.com/@ScaleUpSaaS?sub_confirmation=1) 250 | 2. Buy me A [coffee ❤️](https://www.buymeacoffee.com/scaleupsaas) 251 | 252 | Thanks for your support :) 253 | 254 | 255 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "saasscaleup/laravel-console-log", 3 | "description": "Easily stream your Laravel application logs to the browser console.log in real-time using server-sent event (SSE)", 4 | "license": "MIT", 5 | "type": "library", 6 | "authors": [ 7 | { 8 | "name": "Scale-Up SaaS", 9 | "email": "saasscaleup@gmail.com" 10 | } 11 | ], 12 | "homepage": "https://github.com/saasscaleup/laravel-console-log", 13 | "keywords": [ 14 | "Laravel", 15 | "SSE", 16 | "Sever Sent Events", 17 | "SeverEvents", 18 | "Server", 19 | "Event", 20 | "Realtime", 21 | "Polling", 22 | "Notifications", 23 | "Stream Log", 24 | "Log", 25 | "Laravel Log", 26 | "Debug", 27 | "Laravel Debug", 28 | "Console log" 29 | ], 30 | "require": { 31 | "php": "^7.0|^8.0", 32 | "illuminate/support": "~5|~6|~7|~8|~9|~10|~11" 33 | }, 34 | "autoload": { 35 | "psr-4": { 36 | "Saasscaleup\\LCL\\": "src/" 37 | }, 38 | "files": [ 39 | "src/Helpers/helper.php" 40 | ] 41 | }, 42 | "extra": { 43 | "laravel": { 44 | "providers": [ 45 | "Saasscaleup\\LCL\\LCLServiceProvider" 46 | ], 47 | "aliases": { 48 | "LCL": "Saasscaleup\\LCL\\Facades\\LCLFacade" 49 | } 50 | } 51 | }, 52 | "config": { 53 | "sort-packages": true 54 | }, 55 | "minimum-stability": "stable" 56 | } 57 | -------------------------------------------------------------------------------- /lcl-demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saasscaleup/laravel-console-log/d92e0a8f2675404d09923d936794696028224e5b/lcl-demo.gif -------------------------------------------------------------------------------- /lcl-saasscaleup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saasscaleup/laravel-console-log/d92e0a8f2675404d09923d936794696028224e5b/lcl-saasscaleup.png -------------------------------------------------------------------------------- /src/.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: saasscaleup 4 | ko_fi: scaleupsaas 5 | custom: https://buymeacoffee.com/scaleupsaas -------------------------------------------------------------------------------- /src/Config/lcl.php: -------------------------------------------------------------------------------- 1 | env('LCL_ENABLED', true), 7 | 8 | // enable or disable laravel log listener 9 | 'log_enabled' => env('LCL_LOG_ENABLED', true), 10 | 11 | // log listener for specific log type 12 | 'log_type' => env('LCL_LOG_TYPE', 'info,error,warning,alert,critical,debug,success'), // Without space 13 | 14 | // log listener for specific word inside log messages 15 | 'log_specific' => env('LCL_LOG_SPECIFIC', ''), // 'test' or 'foo' or 'bar' or leave empty '' to anyable any word 16 | 17 | // echo data loop in LCLController 18 | 'interval' => env('LCL_INTERVAL', 1), 19 | 20 | // append logged user id in LCL response 21 | 'append_user_id' => env('LCL_APPEND_USER_ID', true), 22 | 23 | // keep events log in database 24 | 'keep_events_logs' => env('LCL_KEEP_EVENTS_LOGS', false), 25 | 26 | // Frontend pull invoke interval 27 | 'server_event_retry' => env('LCL_SERVER_EVENT_RETRY', '2000'), 28 | 29 | // every 5 minutes cache expired, delete logs on next request 30 | 'delete_log_interval' => env('LCL_DELETE_LOG_INTERVAL', 300), 31 | 32 | /******* Frontend *******/ 33 | 34 | // eanlbed console log on browser 35 | 'js_console_log_enabled' => env('LCL_JS_CONSOLE_LOG_ENABLED', true), 36 | 37 | ]; 38 | -------------------------------------------------------------------------------- /src/Facades/LCLFacade.php: -------------------------------------------------------------------------------- 1 | notify($message, $type, $event); 13 | } 14 | } 15 | 16 | if (!function_exists('getVisitorId')) { 17 | 18 | /** 19 | * getVisitorId 20 | * 21 | * @return md5 22 | */ 23 | function getVisitorId(){ 24 | $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; 25 | $visitor_ip = getVisitorIp(); 26 | 27 | return md5(php_uname('m') . $user_agent .$visitor_ip); 28 | } 29 | } 30 | 31 | if (!function_exists('getVisitorIp')) { 32 | 33 | /** 34 | * getVisitorIp 35 | * 36 | * @return string 37 | */ 38 | function getVisitorIp() 39 | { 40 | foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) { 41 | if (array_key_exists($key, $_SERVER) === true) { 42 | foreach (explode(',', $_SERVER[$key]) as $ip) { 43 | $ip = trim($ip); // just to be safe 44 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) { 45 | return $ip; 46 | } 47 | } 48 | } 49 | } 50 | return ''; // it will return empty string if Ip not found 51 | } 52 | } -------------------------------------------------------------------------------- /src/Http/Controllers/LCLController.php: -------------------------------------------------------------------------------- 1 | where('client',$client)->where('delivered', false)->oldest()->first()) { 37 | 38 | // if the connection has been closed by the client we better exit the loop 39 | if (connection_aborted()) { 40 | return; 41 | } 42 | 43 | $data = json_encode([ 44 | 'message' => $model->message, 45 | 'type' => strtolower($model->type), 46 | 'time' => date('H:i:s', strtotime($model->created_at)), 47 | ]); 48 | 49 | echo 'id: ' . $model->id . "\n"; 50 | echo 'event: ' . $model->event . "\n"; 51 | echo 'data: ' . $data . "\n\n"; 52 | 53 | ob_flush(); 54 | flush(); 55 | // sleep(config('lcl.interval')); 56 | sleep(1); 57 | $model->delivered = '1'; 58 | $model->save(); 59 | } 60 | 61 | echo ": heartbeat\n\n"; 62 | 63 | }); 64 | $response->headers->set('Content-Type', 'text/event-stream'); 65 | $response->headers->set('X-Accel-Buffering', 'no'); 66 | $response->headers->set('Cach-Control', 'no-cache'); 67 | $response->headers->set('Connection', 'keep-alive'); 68 | return $response; 69 | 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/Http/routes.php: -------------------------------------------------------------------------------- 1 | 'Saasscaleup\LCL\Http\Controllers', 6 | 'prefix' => 'lcl' 7 | ], 8 | static function () { 9 | 10 | Route::get('stream_console_log', 'LCLController@stream')->name('lcl-stream-log'); 11 | } 12 | ); 13 | -------------------------------------------------------------------------------- /src/LCL.php: -------------------------------------------------------------------------------- 1 | StreamConsoleLog = $StreamConsoleLog; 26 | } 27 | 28 | /** 29 | * Notify LCL event. 30 | * 31 | * @param string $message : notification message 32 | * @param string $type : alert, success, error, warning, info, debug, critical, etc... 33 | * @param string $event : Type of event such as "EmailSent", "UserLoggedIn", etc 34 | * @return bool 35 | */ 36 | public function notify(string $message, string $type = 'info', string $event = 'stream-console-log'): bool 37 | { 38 | return $this->StreamConsoleLog->saveEvent($message, $type, $event); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/LCLServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->routesAreCached()) { 20 | require __DIR__ . '/Http/routes.php'; 21 | } 22 | 23 | $this->loadViewsFrom(__DIR__ . '/Views', 'lcl'); 24 | 25 | // Publishing is only necessary when using the CLI. 26 | if ($this->app->runningInConsole()) { 27 | // Publish the configuration file. 28 | $this->publishes([ 29 | __DIR__ . '/Config/lcl.php' => config_path('lcl.php'), 30 | ], 'lcl.config'); 31 | 32 | // Publish the views. 33 | $this->publishes([ 34 | __DIR__ . '/Views' => base_path('resources/views/vendor/lcl'), 35 | ], 'lcl.views'); 36 | 37 | // Publish the migrations. 38 | $this->publishes([ 39 | __DIR__ . '/Migrations' => database_path('migrations') 40 | ]); 41 | } 42 | 43 | // If Log listener enabled 44 | if (config('lcl.log_enabled')){ 45 | 46 | // register event handler 47 | Event::listen(MessageLogged::class, function (MessageLogged $e) { 48 | 49 | // If log type in array 50 | if (in_array($e->level,explode(',',config('lcl.log_type')))){ 51 | 52 | 53 | $message = empty($e->context) ? $e->message : $e->message.' : '.json_encode($e->context); 54 | 55 | if (config('lcl.log_specific')!==''){ 56 | if (str_contains($message,config('lcl.log_specific')) ){ 57 | stream_console_log($message,$e->level,'stream-console-log'); 58 | } 59 | }else{ 60 | stream_console_log($message,$e->level,'stream-console-log'); 61 | } 62 | } 63 | }); 64 | 65 | } 66 | } 67 | 68 | /** 69 | * Register package services. 70 | * 71 | * @return void 72 | */ 73 | public function register() 74 | { 75 | $this->mergeConfigFrom(__DIR__ . '/Config/lcl.php', 'lcl'); 76 | 77 | // Register the service package provides. 78 | $this->app->singleton('LCL', function () { 79 | return $this->app->make(LCL::class); 80 | }); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/Migrations/2023_12_05_99999_create_stream_console_logs_table.php: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 18 | $table->unsignedInteger('user_id')->default(0); 19 | $table->longText('message'); 20 | $table->string('event', 50); 21 | $table->string('type', 50); 22 | $table->boolean('delivered')->default(0); 23 | $table->string('client', 50)->nullable(); 24 | $table->timestamps(); 25 | }); 26 | 27 | Schema::table('stream_console_logs', static function (Blueprint $table) { 28 | $table->index(['client','delivered']); 29 | $table->index(['delivered']); 30 | }); 31 | } 32 | 33 | /** 34 | * Reverse the migrations. 35 | * 36 | * @return void 37 | */ 38 | public function down() 39 | { 40 | Schema::drop('stream_console_logs'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Models/StreamConsoleLog.php: -------------------------------------------------------------------------------- 1 | deleteProcessed(); 33 | 34 | $date = new DateTime; 35 | $data['message'] = $message; 36 | $data['event'] = $event; 37 | $data['type'] = $type; 38 | $data['client'] = getVisitorId(); 39 | $data['created_at'] = $date->format('Y-m-d H:i:s'); 40 | $data['updated_at'] = $date->format('Y-m-d H:i:s'); 41 | 42 | if (config('lcl.append_user_id') && auth()->check()) { 43 | $data['user_id'] = auth()->user()->getAuthIdentifier(); 44 | } 45 | 46 | return self::query()->insert($data); 47 | } 48 | 49 | /** 50 | * Delete already processed events 51 | */ 52 | public function deleteProcessed() 53 | { 54 | if (config('lcl.keep_events_logs')) { 55 | return false; 56 | } 57 | 58 | if (Cache::has('delete_stream_log') ){ 59 | return false; 60 | } 61 | 62 | // Delete delivered messages 63 | self::query()->where('delivered', true)->delete(); 64 | 65 | // delete old not delivered messages 66 | $date = new DateTime; 67 | $date = $date->modify('-30 minutes'); 68 | self::query()->where('delivered', false)->where('created_at','<=',$date->format('Y-m-d H:i:s'))->delete(); 69 | 70 | // set cache 71 | Cache::put('delete_stream_log',true,config('lcl.delete_log_interval')); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/Views/view.blade.php: -------------------------------------------------------------------------------- 1 | @if(config('lcl.enabled') && config('app.env')!=='production')) 2 | 3 | 4 | 5 | 6 | 7 | 35 | 36 | @endif --------------------------------------------------------------------------------