composer require netzindianer/laravel-file-notifier
php artisan vendor:publish --provider="Netzindianer\FileNotifier\FileNotifierProvider"
This module will check config/file-notifier.php
, and if it finds config for any of senders, it will execute them
php artisan file-notifier:default
0 * * * * php artisan file-notifier:default >> /var/log/my_laravel_app_file_notifier.log
// Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule
->command('file-notifier:default')
->hourly()
->appendOutputTo(storage_path('logs/file-notifier.log'));
}
This module will use Laravel Mailable to send raw logs in mail content to specified addresses.
php artisan file-notifier:email \
--file-name=/var/www/html/storage/logs/laravel.log \
--seconds=3600 \
--lines=300 \
[email protected] \
[email protected] \
--subject="My Laravel App - laravel.log"
0 * * * * php artisan file-notifier:email --file-name=/var/www/html/storage/logs/laravel.log --seconds=3600 --lines=300 [email protected] [email protected] --subject="My Laravel App - laravel.log" >> /var/log/my_laravel_app_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'));
}
This module will send logs as attachment to given webhook Documentation: Here
php artisan 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\"}"
0 * * * * php artisan 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\"}" >> /var/log/my_laravel_app_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'));
}
As example here is class, which will send logs in POST field to specified url File Notifier is using xtompie/result package to handle result
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();
}
}
If you want, for example, call file-notifier:email
but read addresses from database, api works exactly as above,
so for emails you could do this:
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,
);
}
}
All notifiers in this package returns xtompie results with:
-1
if file not exists or there is nothing to sendNULL
if everything went correct- Failure with exception message if something went wrong