PHP code example of maksudur-dev / laravel-logpilot

1. Go to this page and download the library: Download maksudur-dev/laravel-logpilot 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/ */

    

maksudur-dev / laravel-logpilot example snippets


return [
    'enabled' => env('LOGPILOT_ENABLED', true),
    'driver' => env('LOGPILOT_DRIVER', 'log'), // log, database, both
    'channel' => env('LOGPILOT_CHANNEL', null), // default log channel
    'auto_group_id' => true,
    'store_request_context' => true,
    'api_enabled' => env('LOGPILOT_API_ENABLED', false),
    'queue' => env('LOGPILOT_QUEUE', false),
    'retention_days' => 30,
];

activity('user_login');

// With details
activity('profile_updated', $user, 'User updated their profile photo');

// With array message (automatically cast to JSON)
activity('api_response', null, [
    'status' => 'success',
    'data' => ['id' => 1]
]);

// Full control
activity(
    action: 'withdraw_request',
    model: $user,
    message: 'Gateway timeout',
    level: 'error',
    meta: ['amount' => 500]
);

use LaravelLogPilot\Traits\LogsActivity;

class Order extends Model
{
    use LogsActivity;
}

// Now you can do:
$order->logActivity('shipped', 'Order has been shipped to customer');

// Retrieve logs for this model:
$logs = $order->activities;

use LaravelLogPilot\Services\ActivityLogger;

class PaymentService
{
    public function __construct(protected ActivityLogger $logger) {}

    public function process()
    {
        $this->logger->log('payment_started');
    }
}

activity_group('checkout_flow_001');

activity('cart_validated');
activity('payment_processing');
activity('order_created'); // All will share log_id: checkout_flow_001
bash
php artisan activity:install
bash
php artisan migrate
blade
{{-- In your controller --}}
$logs = $order->activities()->paginate(10);
return view('orders.show', compact('order', 'logs'));

{{-- In orders/show.blade.php --}}
<div class="activity-feed">
    @foreach($logs as $log)
        <div class="log-item {{ $log->level }}">
            <strong>{{ $log->action }}</strong>
            <div>
                @if(is_array($log->message))
                    <pre>{{ json_encode($log->message, JSON_PRETTY_PRINT) }}</pre>
                @else
                    {{ $log->message }}
                @endif
            </div>
            <small>{{ $log->created_at->diffForHumans() }}</small>
        </div>
    @endforeach
    {{ $logs->links() }}
</div>