PHP Version of console.log() for Laravel
Tags: laravel, opensource, php, github
Published: 2024-06-27 21:38:54

**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](https://github.com/saasscaleup/laravel-console-log/blob/master/lcl-demo.gif?raw=true)

---

## ✨ 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.

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

### For Laravel < 5.5

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

```php
Saasscaleup\LCL\LCLServiceProvider::class,
```

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

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

## Configuration

Publish Config, Migration, and View Files

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

### Run Migration

Create the `stream_console_logs` table:

```bash
php artisan migrate
```

### Setup LCL in Your View/Layout

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

```php
@include('lcl::view')
```

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

## Environment Configuration

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

```php
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),

];
```

## Usage

Stream notifications from your controllers or event classes:

```php
use Saasscaleup\LCL\Facades\LCLFacade;

public function myFunction()
{
    LCLFacade::notify('Message 1');
    LCLFacade::notify('Message 2', 'success');
    LCLFacade::notify('Message 3', 'error');
}
```

Or use the helper function:

```php
stream_log('Message 1');
stream_log('Message 2');
```

Log messages using Laravel's logging system:

```php
\Log::info('Log Message 1');
\Log::error('Log Message 2');
```

## 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`:

```php
LCLFacade::notify('User purchased a plan', 'info', 'UserPurchase');
```

Handle it in your view:

```javascript
<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>
```

## Github link

Check out [laravel-console-log](https://github.com/saasscaleup/laravel-console-log) package Github repository 


## License

This package is open-sourced software licensed under the [MIT license](license.md).