PHP code example of nightshift-foundry / laravel-alertstream
1. Go to this page and download the library: Download nightshift-foundry/laravel-alertstream 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/ */
nightshift-foundry / laravel-alertstream example snippets
use NightshiftFoundry\AlertStream\Channels\Contracts\AlertChannel;
use Throwable;
class PagerDutyChannel implements AlertChannel
{
public function send(string $title, Throwable $exception, array $context): void
{
// deliver your alert via HTTP call, SDK, or whatever you need
}
}
// App\Providers\AppServiceProvider (or any service provider)
public function register(): void
{
$this->app->bind(PagerDutyChannel::class);
$this->app->tag([PagerDutyChannel::class], 'alertstream.channel');
}
'mute' => [
// defaults are listed above, list is fully customisable
\Illuminate\Auth\AuthenticationException::class,
\Illuminate\Auth\AuthorizationException::class,
\Illuminate\Validation\ValidationException::class,
\Illuminate\Http\Exceptions\HttpResponseException::class,
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
\Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException::class,
// your own additions:
\App\Exceptions\ExpectedBusinessException::class,
],
use NightshiftFoundry\AlertStream\Exceptions\Handler as AlertStreamHandler;
public function boot(): void
{
$this->app->make(AlertStreamHandler::class)
->report(\Illuminate\Auth\AuthenticationException::class);
}
'snapshots' => [
'enabled' => env('ALERTSTREAM_SNAPSHOTS', false),
'table' => env('ALERTSTREAM_SNAPSHOTS_TABLE', 'alertstream_snapshots'),
'retention_days' => env('ALERTSTREAM_SNAPSHOTS_RETENTION', 30),
'route_prefix' => env('ALERTSTREAM_SNAPSHOTS_ROUTE_PREFIX', 'alertstream'),
'route_middleware' => ['web'], // add 'auth' if you want login-protected access
],
// app/Console/Kernel.php or routes/console.php (Laravel 11+)
$schedule->command('model:prune')->daily();
// app/Console/Kernel.php or routes/console.php (Laravel 11+)
$schedule->command('alertstream:prune-snapshots')->daily();
use NightshiftFoundry\AlertStream\Facades\AlertStream;
AlertStream::report('Payment gateway timeout', $exception, ['order_id' => 42]);
AlertStream::log(string $level, string $message, mixed $data = null, array $context = []);
use NightshiftFoundry\AlertStream\Facades\AlertStream;
AlertStream::log('debug', 'Slow query detected', ['sql' => $query, 'time_ms' => 320]);
AlertStream::log('info', 'User exported report', ['user_id' => $user->id, 'rows' => 1_200]);
AlertStream::log('warning', 'Disk usage above 80%', ['disk' => '/dev/sda1', 'usage' => '82%']);
AlertStream::log('error', 'Redis connection lost, falling back to file cache');
AlertStream::log('critical', 'Queue worker stalled', ['queue' => 'payments', 'pending' => 847]);
AlertStream::log('emergency', 'All database connections exhausted');
// These two calls are identical:
AlertStream::debug('Cache miss', ['key' => 'user:42']);
AlertStream::log('debug', 'Cache miss', ['key' => 'user:42']);
use NightshiftFoundry\AlertStream\Services\AlertStreamService;
class OrderService
{
public function __construct(private AlertStreamService $alertStream) {}
public function charge(): void
{
try {
// ...
} catch (Throwable $e) {
$this->alertStream->report('Charge failed', $e, ['order_id' => $this->id]);
}
}
}
// config/alertstream.php
'severity_map' => [
\App\Exceptions\PaymentFailedException::class => 'critical',
\App\Exceptions\RateLimitException::class => 'warning',
],
// app/AlertStream/AddGitSha.php
class AddGitSha
{
public function __invoke(array $context, \Throwable $e): array
{
$context['git_sha'] = config('app.git_sha');
return $context;
}
}
// config/alertstream.php
'context_enrichers' => [
\App\AlertStream\AddGitSha::class,
\App\AlertStream\AddTenantId::class,
],
use Illuminate\Notifications\Notification;
use NightshiftFoundry\AlertStream\Channels\AlertStreamNotificationChannel;
class PaymentFailed extends Notification
{
public function via($notifiable): array
{
return [AlertStreamNotificationChannel::class, 'mail'];
}
public function toAlertStream($notifiable): array
{
return [
'message' => 'Payment failed for order #' . $this->order->id,
'exception' => $this->exception, // optional
'context' => ['amount' => $this->order->total],
];
}
}
bash
php artisan vendor:publish --tag=alertstream-config
bash
php artisan migrate
bash
php artisan alertstream:prune-snapshots # uses configured retention (default: 30 days)
php artisan alertstream:prune-snapshots --days=7 # override
bash
php artisan vendor:publish --tag=alertstream-views
src/
├── Channels/
│ ├── Contracts/
│ │ └── AlertChannel.php <- implement this to add any channel
│ ├── AlertStreamNotificationChannel.php
│ ├── SlackChannel.php
│ ├── TeamsChannel.php
│ ├── DiscordChannel.php
│ └── MailChannel.php
├── Commands/
│ ├── TestAlertCommand.php
│ └── PruneSnapshotsCommand.php
├── Events/
│ └── ExceptionCaptured.php
├── Exceptions/
│ ├── AlertStreamException.php
│ └── Handler.php
├── Http/
│ └── Controllers/
│ ├── HealthController.php
│ └── SnapshotController.php
├── Listeners/
│ └── SendExceptionToAlertStream.php
├── Models/
│ └── Snapshot.php
├── Providers/
│ └── AlertStreamServiceProvider.php
└── Services/
├── AlertStreamService.php
├── SnapshotService.php
└── ThrottleService.php
database/
└── migrations/
└── create_alertstream_snapshots_table.php
resources/
└── views/
└── snapshots/
├── index.blade.php
└── show.blade.php
routes/
└── alertstream.php