Installation:
composer require applogger/sdk-core
Requires PHP 8.3+.
Initialize the SDK (in your application bootstrap, e.g., bootstrap/app.php or index.php):
use AppLogger\SdkCore\Client;
$client = Client::initialize('YOUR_API_KEY');
Replace YOUR_API_KEY with your AppLogger project key (found in the dashboard).
First Use Case: Capture an Exception
try {
// Risky operation
$result = riskyOperation();
} catch (\Exception $e) {
$client->captureException($e);
}
The exception will be sent to AppLogger with automatic fingerprinting and deduplication.
First Use Case: Log a Message
$logger = $client->logger();
$logger->error('User login failed', [
'user_id' => 123,
'ip' => $_SERVER['REMOTE_ADDR'],
]);
Logs will be structured and sent to AppLogger’s log aggregation pipeline.
Automatic Capture: Enable the global error handler via default_integrations (enabled by default):
$client = Client::initialize('API_KEY', [
'default_integrations' => [
'error_handler' => true,
'shutdown_handler' => true,
],
]);
MemoryReservation to preserve error context during shutdown.Manual Capture: For controlled scenarios:
$client->captureException($exception, [
'tags' => ['auth', 'failed'],
'user' => ['id' => 123, 'email' => '[email protected]'],
]);
LoggerInterface for structured logs:
$logger = $client->logger();
$logger->info('Order processed', [
'order_id' => $order->id,
'amount' => $order->amount,
'metadata' => $order->metadata,
]);
configureScope:
$client->configureScope([
'user_id' => auth()->id(),
'request_id' => request()->header('X-Request-ID'),
]);
$client->addBreadcrumb('Button clicked', [
'button' => 'submit',
'timestamp' => time(),
]);
withScope for temporary context:
$client->withScope(['feature_flag' => true], function () {
// Logs/scopes here will include feature_flag=true
$client->captureMessage('Feature X triggered');
});
The SDK auto-detects your runtime (CLI, FastCGI, Swoole, etc.) and flushes logs/errors appropriately:
flush().Force an immediate flush (e.g., for CLI scripts):
$client->flush();
$client = Client::initialize('API_KEY', [
'data_scrubbing' => [
'fields' => [
'password' => true,
'api_key' => true,
'token' => true,
],
],
]);
$client->addDataScrubber(function ($value) {
return str_repeat('*', strlen($value));
}, 'credit_card');
While this is framework-agnostic, Laravel users can:
AppServiceProvider:
public function boot()
{
$client = Client::initialize(config('applogger.key'));
$this->app->singleton('applogger', fn() => $client);
}
public function handle($request, Closure $next)
{
$client = app('applogger');
$client->configureScope([
'user_id' => auth()->id(),
'path' => $request->path(),
]);
return $next($request);
}
public function report(Throwable $exception)
{
app('applogger')->captureException($exception);
}
The SDK uses PSR-18 for HTTP requests. Customize the client (e.g., for proxies):
use AppLogger\SdkCore\Client;
use Http\Client\Common\Plugin\HeaderAppendPlugin;
use Http\Client\Common\Plugin\HeaderSetPlugin;
use Http\Client\Common\PluginClient;
$httpClient = new PluginClient(
new \GuzzleHttp\Client(),
[
new HeaderSetPlugin('User-Agent', 'MyApp/1.0'),
]
);
$client = Client::initialize('API_KEY', [
'http_client' => $httpClient,
]);
Shutdown Handler Conflicts:
$client = Client::initialize('API_KEY', [
'default_integrations' => [
'shutdown_handler' => false,
],
]);
Memory Leaks in Long-Running Processes:
flush() periodically or disable buffering:
$client = Client::initialize('API_KEY', [
'buffer_events' => false,
]);
Sensitive Data in Breadcrumbs:
// Bad: Adds token to every subsequent event
$client->addBreadcrumb('API call', ['token' => $user->api_token]);
// Good: Scrub or omit sensitive data
$client->addBreadcrumb('API call', ['endpoint' => '/user/data']);
FastCGI/FPM Timeouts:
flush() in a deferred block (Laravel):
\Illuminate\Support\Facades\Deferred::after(function () {
app('applogger')->flush();
});
Double Initialization:
Client::initialize() multiple times creates separate instances. Use a singleton pattern (e.g., Laravel’s app() binding) to reuse the client.Enable Debug Logging:
debug config to log SDK activity to stderr:
$client = Client::initialize('API_KEY', [
'debug' => true,
]);
Validate API Key:
$client->captureMessage('Test event', ['key' => 'dummy']);
Network Issues:
http_client errors in debug logs.$client = Client::initialize('API_KEY', [
'retry_policy' => ['max_attempts' => 0],
]);
Event Deduplication:
fingerprint config (default: SHA-256 of the message).captureException is called multiple times for the same exception.Environment-Specific Config:
$client = Client::initialize(env('APPLOGGER_KEY'));
Performance Optimization:
How can I help you explore Laravel packages today?