Skip to content

ElkLogger

Viames Marino edited this page Feb 23, 2026 · 1 revision

Pair framework: ElkLogger

Pair\Services\ElkLogger sends structured logs to Elasticsearch/ELK.

When to use

Use it for centralized observability, production diagnostics, and log analytics in Kibana.

Main methods

  • __construct(string $elkUrl, ?string $elkUser = null, ?string $elkPassword = null)
  • log(string $message, string $level = 'info', array $context = []): void

Implementation examples

Basic usage

$elk = new \Pair\Services\ElkLogger(
    getenv('ELK_URL'),
    getenv('ELK_USER') ?: null,
    getenv('ELK_PASSWORD') ?: null
);

$elk->log('Order exported', 'info', ['orderId' => 2241]);

Error path with context

try {
    (new ImportService())->run();
} catch (\Throwable $e) {
    $elk->log('Import failed', 'error', [
        'exception' => $e->getMessage(),
        'traceId' => $traceId
    ]);
    throw $e;
}

Common pitfalls

  • Sending unstructured strings limits queryability in ELK.
  • Credentials/timeouts should be configured per environment.

See also: Log, LogBar, ErrorLog.

Clone this wiki locally