PHP code example of micrologs / laravel
1. Go to this page and download the library: Download micrologs/laravel 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/ */
micrologs / laravel example snippets
use Micrologs\Laravel\Facades\Micrologs;
Micrologs::error('Payment failed', ['severity' => 'critical']);
Micrologs::audit('order.placed', $user->email, ['order_id' => $order->id]);
use Micrologs\Laravel\MicrologsClient;
class CheckoutController extends Controller
{
public function store(Request $request, MicrologsClient $micrologs)
{
try {
// process order
} catch (\Exception $e) {
$micrologs->error($e->getMessage(), [
'type' => 'CheckoutError',
'severity' => 'critical',
'context' => ['order_id' => $request->order_id],
]);
throw $e;
}
}
}
->withMiddleware(function (Middleware $middleware) {
$middleware->append(\Micrologs\Laravel\Middleware\TrackErrors::class);
})
protected $middleware = [
\Micrologs\Laravel\Middleware\TrackErrors::class,
];
Micrologs::error('Something went wrong', [
'type' => 'PaymentError', // groups errors of same type together
'severity' => 'critical', // info | warning | error | critical
'file' => __FILE__,
'line' => __LINE__,
'stack' => $e->getTraceAsString(),
'url' => request()->fullUrl(),
'environment' => app()->environment(), // auto-detected if omitted
'context' => ['order_id' => 123], // any extra data, capped at 8KB
]);
// action (:audit('user.login', $user->email, ['role' => 'admin', 'ip' => request()->ip()]);
Micrologs::audit('order.placed', $user->email, ['order_id' => $order->id, 'amount' => 2999]);
Micrologs::audit('settings.updated', $user->email);
Micrologs::audit('api_key.rotated', '[email protected] ');
$result = Micrologs::createLink('https://yourdomain.com/pricing', 'Pricing CTA');
// $result['data']:
// [
// 'code' => 'aB3xYz12',
// 'short_url' => 'https://analytics.yourdomain.com/api/redirect.php?c=aB3xYz12',
// 'destination_url' => 'https://yourdomain.com/pricing',
// 'label' => 'Pricing CTA',
// ]
$result = Micrologs::getLink('aB3xYz12');
// Returns link details including total_clicks
// Any combination of fields - all optional except code
Micrologs::editLink('aB3xYz12', [
'destination_url' => 'https://yourdomain.com/new-page',
'label' => 'Updated CTA',
'is_active' => false,
]);
Micrologs::deleteLink('aB3xYz12');
$analytics = Micrologs::analytics();
$result = Micrologs::analytics()->visitors(['range' => '30d']);
// $result['data']:
// [
// 'unique_visitors' => 1842,
// 'total_pageviews' => 5631,
// 'total_sessions' => 2109,
// 'bounce_rate' => 43.2,
// 'over_time' => [...],
// ]
Micrologs::analytics()->returning(['range' => '30d']);
Micrologs::analytics()->sessions(['range' => '7d']);
// avg_duration_seconds, avg_duration_engaged, avg_pages_per_session, over_time
Micrologs::analytics()->pages(['range' => '30d']);
Micrologs::analytics()->devices(['range' => '30d']);
Micrologs::analytics()->locations(['range' => '30d']);
Micrologs::analytics()->referrers(['range' => '30d']);
Micrologs::analytics()->utm(['range' => '30d']);
// All error groups
Micrologs::analytics()->errors(['range' => '30d']);
// Filtered
Micrologs::analytics()->errors([
'range' => '30d',
'status' => 'open',
'severity' => 'critical',
'environment' => 'production',
]);
// Daily trend
Micrologs::analytics()->errorsTrend(['range' => '30d']);
// Trend for a single group
Micrologs::analytics()->errorsTrend(['range' => '30d', 'group_id' => 12]);
// Full detail for one group
Micrologs::analytics()->errorDetail(['id' => 12]);
// Single group
Micrologs::updateErrorStatus(42, 'investigating');
Micrologs::updateErrorStatus(42, 'resolved');
// Bulk
Micrologs::updateErrorStatus([12, 15, 22], 'ignored');
Micrologs::analytics()->audits(['range' => '7d']);
// Filtered
Micrologs::analytics()->audits([
'range' => '30d',
'action' => 'user.login',
'actor' => '[email protected] ',
]);
Micrologs::analytics()->links(['range' => '30d']);
Micrologs::analytics()->linkDetail(['code' => 'aB3xYz12', 'range' => '30d']);
Micrologs::analytics()->visitors([
'range' => 'custom',
'from' => '2026-01-01',
'to' => '2026-01-31',
]);
$result = Micrologs::verify('some_key');
$result = Micrologs::error('Payment failed');
if ($result === null) {
// SDK call failed - check your Laravel log
// Your application continues normally regardless
}
bash
php artisan vendor:publish --tag=micrologs-config