minhyung / monolog-openobserve
Monolog handler for OpenObserve
Requires
- php: >=8.3
- ext-curl: *
- monolog/monolog: ^3.0
- psr/log: ^2.0 || ^3.0
Requires (Dev)
- fakerphp/faker: ^1.24
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Monolog handler for OpenObserve
Requirements
- PHP 8.3 or later
- the
curlextension - Monolog 3
Installation
composer require minhyung/monolog-openobserve
Usage
use Minhyung\Monolog\OpenObserveHandler; use Monolog\Logger; $handler = new OpenObserveHandler( host: 'http://localhost:5080', organizationId: 'default', streamName: 'monolog', username: 'admin@yourdomain.com', password: 'yourpassword', ); $logger = new Logger('app'); $logger->pushHandler($handler); $logger->info('Hello OpenObserve!', ['foo' => 'bar']);
Handling failures
By default the handler throws when OpenObserve cannot be reached or rejects the
request, including on an authentication failure such as 401 or when an HTTP
success response reports rejected records in status[].failed. Set
ignoreFailure: true to swallow those errors so that a logging problem never
breaks the application:
$handler = new OpenObserveHandler( host: 'http://localhost:5080', organizationId: 'default', streamName: 'monolog', username: 'admin@yourdomain.com', password: 'yourpassword', ignoreFailure: true, );
Swallowed failures disappear silently. To keep a record of them, pass a
fallbackLogger — any PSR-3 logger — and every ignored failure is reported to it
at error level, with the original exception under the exception context key:
use Monolog\Handler\ErrorLogHandler; $handler = new OpenObserveHandler( host: 'http://localhost:5080', organizationId: 'default', streamName: 'monolog', username: 'admin@yourdomain.com', password: 'yourpassword', ignoreFailure: true, fallbackLogger: new Logger('openobserve', [new ErrorLogHandler()]), );
The fallback logger must not write back to this handler, as that would recurse.
It is only consulted while ignoreFailure is on; when the handler rethrows, the
caller already sees the exception.
Timeouts
Because a stalled or unreachable OpenObserve would otherwise block the
application on every log call, the request is bounded by two timeouts: 2 seconds
to connect and 5 seconds for the whole request. Adjust them — or set the request
timeout to 0 to disable it — via the connectTimeout and timeout arguments:
$handler = new OpenObserveHandler( host: 'http://localhost:5080', organizationId: 'default', streamName: 'monolog', username: 'admin@yourdomain.com', password: 'yourpassword', connectTimeout: 2.0, timeout: 5.0, );
Each log or batch is sent once, without automatic retries, so retries do not multiply the configured timeout. A timeout does not guarantee that the server has not already ingested the records; retrying may create duplicates.