PHP code example of central-logs / laravel
1. Go to this page and download the library: Download central-logs/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/ */
central-logs / laravel example snippets
protected $middleware = [
// ... other middleware
\CentralLogs\Http\Middleware\FlushCentralLogsMiddleware::class,
];
'channels' => [
// Option 1: Use Central Logs directly
'central-logs' => [
'driver' => 'monolog',
'handler' => CentralLogs\Handler\CentralLogsHandler::class,
'level' => env('LOG_LEVEL', 'debug'),
],
// Option 2: Add to stack (recommended)
'stack' => [
'driver' => 'stack',
'channels' => ['daily', 'central-logs'],
'ignore_exceptions' => false,
],
],
use Illuminate\Support\Facades\Log;
// Simple logging
Log::info('User logged in');
// With context
Log::info('User logged in', [
'user_id' => $user->id,
'ip' => request()->ip(),
]);
// Error logging with exception
try {
// ... your code
} catch (Exception $e) {
Log::error('Payment processing failed', [
'exception' => $e,
'order_id' => $order->id,
]);
}
// Different log levels
Log::debug('Debugging information');
Log::info('Informational message');
Log::warning('Warning message');
Log::error('Error occurred');
Log::critical('Critical issue');
return [
// API endpoint
'api_url' => env('CENTRAL_LOGS_URL'),
'api_key' => env('CENTRAL_LOGS_API_KEY'),
// Mode: sync or async
'mode' => env('CENTRAL_LOGS_MODE', 'async'),
// Batch configuration
'batch' => [
'enabled' => env('CENTRAL_LOGS_BATCH_ENABLED', true),
'size' => env('CENTRAL_LOGS_BATCH_SIZE', 50),
'timeout' => env('CENTRAL_LOGS_BATCH_TIMEOUT', 5),
'queue' => env('CENTRAL_LOGS_BATCH_QUEUE', 'central-logs'),
],
// Source identifier
'source' => env('CENTRAL_LOGS_SOURCE', env('APP_NAME', 'laravel')),
// HTTP client settings
'http' => [
'timeout' => env('CENTRAL_LOGS_TIMEOUT', 5),
'retry' => [
'times' => env('CENTRAL_LOGS_RETRY_TIMES', 3),
'delay' => env('CENTRAL_LOGS_RETRY_DELAY', 100),
],
'verify_ssl' => env('CENTRAL_LOGS_VERIFY_SSL', true),
],
// Fallback when Central Logs is unavailable
'fallback' => [
'enabled' => env('CENTRAL_LOGS_FALLBACK', true),
'channel' => env('CENTRAL_LOGS_FALLBACK_CHANNEL', 'stack'),
],
// Context enrichment
'context' => [
'
// config/central-logs.php
'context' => [
'custom' => [
'app_version' => '1.2.3',
'datacenter' => 'us-east-1',
'environment_type' => 'staging',
],
],
Log::channel('central-logs')->info('This goes only to Central Logs');
if (app()->environment('production')) {
Log::channel('central-logs')->info('Production log');
} else {
Log::info('Development log');
}
try {
throw new \RuntimeException('Something went wrong');
} catch (\Exception $e) {
Log::error('An error occurred', [
'exception' => $e, // Automatically formatted
'user_id' => auth()->id(),
]);
}
// app/Http/Kernel.php
protected $middleware = [
\CentralLogs\Http\Middleware\FlushCentralLogsMiddleware::class,
];
bash
php artisan vendor:publish --tag=central-logs-config
bash
php artisan central-logs:test
bash
php artisan queue:work --queue=central-logs
bash
# .env
QUEUE_CONNECTION=database
# Create queue tables
php artisan queue:table
php artisan migrate
bash
# Production (with Supervisor)
php artisan queue:work --queue=central-logs --tries=3 --timeout=30
# Development
php artisan queue:listen --queue=central-logs
bash
php artisan central-logs:test
bash
php artisan tinker
app(\CentralLogs\Support\BatchAggregator::class)->flush();