thinkerytim's avatar

Monitor log file "real time" to update app status

I'm working on a project where I need to create a simple front end dashboard that monitors a piece of hardware. The hardware has no API, and the only way I can know what's happening within it is by tailing a log file. The messages written to the log will tell me the device status and other bits of info I need.

I think using laravel is possibly overkill for this, but they have plans to expand the functionality of the app down the road, so I'm thinking starting with laravel may save growing pains later.

My question is what would the best way be in the laravel world to have a monitor process running all the time, watching the log file, and notifying the app of events? Would an artisan process be best? A service provider?

Thanks for any input-- I have some ideas but was hoping for some laravel-centric wisdom.

0 likes
1 reply
LaryAI's avatar
Level 58

To monitor a log file in real-time and update your Laravel application accordingly, you can use a combination of Laravel's Artisan commands, event broadcasting, and possibly a queue worker. Here's a step-by-step solution:

  1. Create an Artisan Command: This command will be responsible for tailing the log file and emitting events when new log entries are detected.

  2. Broadcast Events: Use Laravel's event broadcasting to notify the front end of new log entries.

  3. Front End Listening: Use a front-end library like Laravel Echo to listen for these events and update the dashboard in real-time.

Step 1: Create an Artisan Command

First, create a new Artisan command:

php artisan make:command MonitorLogFile

Then, implement the command to tail the log file:

Step 2: Create an Event

Create an event that will be broadcasted when the log file is updated:

php artisan make:event LogFileUpdated

Then, implement the event:

// app/Events/LogFileUpdated.php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class LogFileUpdated implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function broadcastOn()
    {
        return new Channel('log-file');
    }
}

Step 3: Set Up Broadcasting

Ensure you have broadcasting set up in your Laravel application. In your .env file, set the broadcast driver:

BROADCAST_DRIVER=pusher

Configure your broadcasting settings in config/broadcasting.php and set up a Pusher account or another broadcasting service.

Step 4: Front End Listening

Install Laravel Echo and Pusher:

npm install --save laravel-echo pusher-js

Configure Laravel Echo in your resources/js/bootstrap.js:

import Echo from "laravel-echo";
window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true
});

Finally, listen for the event in your front-end JavaScript:

window.Echo.channel('log-file')
    .listen('LogFileUpdated', (e) => {
        console.log(e.message);
        // Update your dashboard UI here
    });

Running the Command

To start monitoring the log file, run the Artisan command:

php artisan monitor:logfile

You can also set up a supervisor to keep this command running in the background.

This setup will allow you to monitor the log file in real-time and update your Laravel application's front end accordingly.

Please or to participate in this conversation.