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/ */
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
use LaravelLogPilot\Enums\LogLevel;
use LaravelLogPilot\Enums\ActivityAction;
activity(
action: ActivityAction::UPDATED->value,
message: 'User profile updated',
level: LogLevel::WARNING
);
use LaravelLogPilot\Services\ActivityService;
use LaravelLogPilot\DataTransferObjects\ActivityQueryDTO;
class ActivityReportController
{
public function __construct(protected ActivityService $activityService) {}
public function index(Request $request)
{
$dto = ActivityQueryDTO::fromRequest($request);
$paginatedLogs = $this->activityService->getPaginatedLogs($dto);
return response()->json($paginatedLogs);
}
}
use LaravelLogPilot\Models\ActivityLog;
// Filtered logs via DTO
$logs = ActivityLog::fetchFiltered($queryDTO);
// Grouped trace logs by log_id
$traceLogs = ActivityLog::fetchByGroup('checkout_flow_001');
// Logs for a specific Eloquent model
$modelLogs = ActivityLog::fetchForModel(User::class, 1, perPage: 15);