PHP code example of netzindianer / laravel-file-notifier

1. Go to this page and download the library: Download netzindianer/laravel-file-notifier library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

netzindianer / laravel-file-notifier example snippets


// Kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule
        ->command('file-notifier:default')
        ->hourly()
        ->appendOutputTo(storage_path('logs/file-notifier.log'));
}

// Kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule
        ->command('file-notifier:email', [
            '--file-name', '/var/www/html/storage/logs/laravel.log',
            '--seconds', '3600',
            '--lines', '300',
            '--email', '[email protected]',
            '--email', '[email protected]',
            '--subject', 'My Laravel App - laravel.log'
        ])
        ->hourly()
        ->appendOutputTo(storage_path('logs/file-notifier.log'));
}

// Kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule
        ->command('file-notifier:discord', [
            '--file-name', '/var/www/html/storage/logs/laravel.log',
            '--seconds', '3600',
            '--lines', '300',
            '--webhook-id', '000000000000000000',
            '--webhook-token', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
            '--message', '{"username":"My Laravel App","content":"laravel.log"}',
        ])
        ->hourly()
        ->appendOutputTo(storage_path('logs/file-notifier.log'));
}

use Netzindianer\FileNotifier\FileNotifier;
use Xtompie\Result\Result;

class HttpPostNotifier 
{
    public function __construct(
        protected FileNotifier $fileNotifier, // This class handles checking if there is any new content in file
        protected HttpPostNotifierSender $sender, // This is our callable to handle sending logs
    ) {}
    
    public function __invoke(string $url): Result
    {
        // Call FileNotifier with appropriate arguments 
        $success = ($this->fileNotifier)(
            fileName: storage_path('logs/laravel.log'),
            seconds: 3600,
            lines: 300,
            sender: $this->sender->url($url),
        );
        return $success;
    }
}

use Illuminate\Http\Client\Factory as HttpFactory;

class HttpPostNotifierSender 
{
    protected string $url;

    public function __construct(
        protected HttpFactory $http,
    ) {}
    
    public function url(string $url): static
    {
        $this->url = $url;
        return $this;
    }
    
    /**
    * @param string $content Last lines of specified file
    * @param string $fileName Name of file which was checked
    * @return bool FileNotifier will return result of this callable with Xtompie\Result\Result
     */
    public function __invoke(string $content, string $fileName): bool
    {
        $response = $this->http->post($this->url, [
            'fileName' => $fileName,
            'lastLinesOfFile' => $content,
        ]);
        return $response->successful();
    }
}

use \Netzindianer\FileNotifier\FileNotifier;
use \Netzindianer\FileNotifier\Email\EmailSender;
use \App\Models\User;
use Xtompie\Result\Result;

class SendNewLogsToDevelopersUtil
{
    public function __construct(
        protected FileNotifier $notifier,
        protected EmailSender $emailSender,
    ) {}

    public function __invoke(): Result
    {
        $users = User::where('is_developer', true)->get();
        $emails = $users->map(fn(User $user) => $user->email);

        return ($this->notifier)(
            fileName: storage_path('logs/laravel.log'),
            seconds: 3600,
            sender: $this->emailSender
                ->emails($emails)
                ->subject("New logs for developers")
            lines: 300,
        );
    }
}
shell
php artisan file-notifier:default