PHP code example of ebilet / common

1. Go to this page and download the library: Download ebilet/common 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/ */

    

ebilet / common example snippets


return [
    // ...
    Ebilet\Common\ServiceProviders\LoggingServiceProvider::class,
];

use Ebilet\Common\Facades\Log;

// Temel loglama
Log::info('Kullanıcı giriş yaptı', ['user_id' => 123]);
Log::error('Veritabanı bağlantı hatası', ['error' => $exception->getMessage()]);
Log::warning('Yavaş sorgu tespit edildi', ['query' => $sql, 'duration' => 2.5]);

// HTTP loglama
Log::logHttpRequest('POST', '/api/users', $headers, $body);
Log::logHttpResponse(200, $responseHeaders, $responseBody, 0.15);

// Performans metrikleri
Log::logPerformance('database_query', 0.05, ['table' => 'users']);

// İş olayları
Log::logBusinessEvent('user_registered', [
    'user_id' => 123,
    'email' => '[email protected]'
]);

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        \Ebilet\Common\Middleware\HttpLoggingMiddleware::class,
    ]);
    
    $middleware->api(append: [
        \Ebilet\Common\Middleware\HttpLoggingMiddleware::class,
    ]);
})

use Ebilet\Common\Facades\Queue;

// Queue bağlantısı
Queue::connect();

// Log gönderimi
Queue::sendLog([
    'message' => 'Test log',
    'level' => 'info',
    'context' => ['test' => true]
], LogMessageType::APPLICATION_INFO);

// Metric gönderimi
Queue::sendMetric([
    'metric' => 'response_time',
    'value' => 0.15,
    'unit' => 'seconds'
]);

// Event gönderimi
Queue::sendEvent([
    'event' => 'user_registered',
    'data' => ['user_id' => 123]
]);

use Ebilet\Common\Enums\LogMessageType;

// Log mesaj tipi belirleme
$messageType = LogMessageType::HTTP_REQUEST;
$logLevel = $messageType->getLogLevel(); // 'info'
$isCritical = $messageType->isCritical(); // false
$isPerformance = $messageType->isPerformance(); // false

return [
    /*
    |--------------------------------------------------------------------------
    | RabbitMQ Configuration
    |--------------------------------------------------------------------------
    */
    'rabbitmq' => [
        'host' => env('RABBITMQ_HOST', 'localhost'),
        'port' => env('RABBITMQ_PORT', 5672),
        'user' => env('RABBITMQ_USER', 'guest'),
        'password' => env('RABBITMQ_PASSWORD', 'guest'),
        'vhost' => env('RABBITMQ_VHOST', '/'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Queue Configuration
    |--------------------------------------------------------------------------
    */
    'queues' => [
        'channels' => [
            'log_messages' => env('EBILET_QUEUE_LOG_CHANNEL', 'log-messages'),
            'metrics' => env('EBILET_QUEUE_METRICS_CHANNEL', 'metrics'),
            'events' => env('EBILET_QUEUE_EVENTS_CHANNEL', 'events'),
        ],
        'settings' => [
            'log_messages' => [
                'durable' => env('EBILET_QUEUE_LOG_DURABLE', true),
                'ttl' => env('EBILET_QUEUE_LOG_TTL', 86400000), // 24 hours
                'max_length' => env('EBILET_QUEUE_LOG_MAX_LENGTH', 10000),
            ],
            'metrics' => [
                'durable' => env('EBILET_QUEUE_METRICS_DURABLE', true),
                'ttl' => env('EBILET_QUEUE_METRICS_TTL', 604800000), // 7 days
                'max_length' => env('EBILET_QUEUE_METRICS_MAX_LENGTH', 50000),
            ],
            'events' => [
                'durable' => env('EBILET_QUEUE_EVENTS_DURABLE', true),
                'ttl' => env('EBILET_QUEUE_EVENTS_TTL', 2592000000), // 30 days
                'max_length' => env('EBILET_QUEUE_EVENTS_MAX_LENGTH', 100000),
            ],
        ],
        'delivery_mode' => env('EBILET_QUEUE_DELIVERY_MODE', 2), // Persistent
    ],

    /*
    |--------------------------------------------------------------------------
    | HTTP Logging Configuration
    |--------------------------------------------------------------------------
    */
    'http_logging' => [
        'enabled' => env('EBILET_HTTP_LOGGING_ENABLED', true),
        'endpoints' => env('EBILET_HTTP_LOGGING_ENDPOINTS', '*'),
        'excluded_paths' => env('EBILET_HTTP_LOGGING_EXCLUDED_PATHS', [
            '/health', '/metrics', '/favicon.ico', '/robots.txt', '/.well-known'
        ]),
        'excluded_methods' => env('EBILET_HTTP_LOGGING_EXCLUDED_METHODS', ['OPTIONS']),
        'sensitive_headers' => env('EBILET_HTTP_LOGGING_SENSITIVE_HEADERS', [
            'authorization', 'cookie', 'x-api-key', 'x-auth-token'
        ]),
        'log_request_body' => env('EBILET_HTTP_LOGGING_REQUEST_BODY', true),
        'log_response_body' => env('EBILET_HTTP_LOGGING_RESPONSE_BODY', false),
        'max_body_size' => env('EBILET_HTTP_LOGGING_MAX_BODY_SIZE', 1024 * 1024),
        'slow_request_threshold' => env('EBILET_HTTP_LOGGING_SLOW_THRESHOLD', 2000),
    ],
];

use Ebilet\Common\Exceptions\LoggingException;

try {
    Log::info('Test message');
} catch (LoggingException $e) {
    // Logging hatası yönetimi
    error_log("Logging error: " . $e->getMessage());
}
bash
php artisan vendor:publish --provider="Ebilet\Common\ServiceProviders\LoggingServiceProvider"