DEV Community

Cover image for PHP Version of console.log() for Laravel
ScaleUp SaaS
ScaleUp SaaS

Posted on

PHP Version of console.log() for Laravel

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

Welcome to Laravel Console Log (LCL)! This package brings real-time logging to your Laravel application, allowing you to stream your logs directly to your browser's console. Perfect for backend developers who want the power of console.log for their PHP projects. Say goodbye to tedious log file hunting and hello to instant insights!

banner


✨ Features

  • Stream Backend Events: Send messages from Controllers, Events, Models, etc., directly to your browser console.
  • Stream Application Logs: View your Laravel logs (storage/logs/laravel.log) in real-time in your browser console.

Requirements

  • PHP >= 7
  • Laravel >= 5

Installation - Via Composer (Dev Environment)

Not recommended for production.

composer require --dev saasscaleup/laravel-console-log
Enter fullscreen mode Exit fullscreen mode

For Laravel < 5.5

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

Saasscaleup\LCL\LCLServiceProvider::class,
Enter fullscreen mode Exit fullscreen mode

Add the Facade to config/app.php in the aliases section:

'LCL' => Saasscaleup\LCL\Facades\LCLFacade::class,
Enter fullscreen mode Exit fullscreen mode

Configuration

Publish Config, Migration, and View Files

php artisan vendor:publish --provider="Saasscaleup\LCL\LCLServiceProvider"
Enter fullscreen mode Exit fullscreen mode

Run Migration

Create the stream_console_logs table:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Setup LCL in Your View/Layout

Add this to your main view/layout (usually layout/app.blade.php) file before </body>:

@include('lcl::view')
Enter fullscreen mode Exit fullscreen mode
<body>
...
@include('lcl::view')
</body>
Enter fullscreen mode Exit fullscreen mode

Environment Configuration

Adjust your .env file or config/lcl.php to customize settings:

return [

    'enabled' => env('LCL_ENABLED', true),
    'log_enabled' => env('LCL_LOG_ENABLED', true),
    'log_type' => env('LCL_LOG_TYPE', 'info,error,warning,alert,critical,debug'),
    'log_specific' => env('LCL_LOG_SPECIFIC', ''),
    'interval' => env('LCL_INTERVAL', 1),
    'append_user_id' => env('LCL_APPEND_USER_ID', true),
    'keep_events_logs' => env('LCL_KEEP_EVENTS_LOGS', false),
    'server_event_retry' => env('LCL_SERVER_EVENT_RETRY', '2000'),
    'delete_log_interval' => env('LCL_DELETE_LOG_INTERVAL', 600),
    'js_console_log_enabled' => env('LCL_JS_CONSOLE_LOG_ENABLED', true),

];
Enter fullscreen mode Exit fullscreen mode

Usage

Stream notifications from your controllers or event classes:

use Saasscaleup\LCL\Facades\LCLFacade;

public function myFunction()
{
    LCLFacade::notify('Message 1');
    LCLFacade::notify('Message 2', 'success');
    LCLFacade::notify('Message 3', 'error');
}
Enter fullscreen mode Exit fullscreen mode

Or use the helper function:

stream_log('Message 1');
stream_log('Message 2');
Enter fullscreen mode Exit fullscreen mode

Log messages using Laravel's logging system:

\Log::info('Log Message 1');
\Log::error('Log Message 2');
Enter fullscreen mode Exit fullscreen mode

Customizing

Customize Notifications

Modify resources/views/vendor/lcl/view.blade.php to change the notification appearance.

Custom Events

Change the event type in LCLFacade::notify:

LCLFacade::notify('User purchased a plan', 'info', 'UserPurchase');
Enter fullscreen mode Exit fullscreen mode

Handle it in your view:

<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>
Enter fullscreen mode Exit fullscreen mode

Github link

Check out laravel-console-log package Github repository

License

This package is open-sourced software licensed under the MIT license.

Top comments (0)