webmafia / fluentlog
Fluent Forward logger for PHP
v0.4.1
2026-03-11 14:27 UTC
Requires
- php: >=7.4.0
- ext-gmp: *
- rybakit/msgpack: ^0.9.1
README
This is a PHP version of Fluentlog, with three important differences:
- Writing logs is not asynchronous.
- Written logs are not buffered.
- Written logs are not retried.
For this reason it's highly recommended to write to a local log collector (preferably FluentBit).
Installation
composer require webmafia/fluentlog
Usage example
<?php use Webmafia\Fluentlog\Logger; use Webmafia\Fluentlog\TcpClient; use Webmafia\Fluentlog\TextClient; require_once('../vendor/autoload.php'); $env = parse_ini_file('.env'); $client = new TcpClient( host: $env['HOST'], useTls: !empty($env['TLS']), sharedKey: $env['SHARED_KEY'], username: $env['USERNAME'], password: $env['PASSWORD'] ); // $client = new TextClient(fopen('php://output', 'w')); $logger = new Logger($client, 'php'); $start = microtime(true); for($i = 0; $i < 10; $i++) { echo $logger->info('hello from php %d', $i) . "\n"; } $end = microtime(true); $dur = $end - $start; echo 'Done in ' . $dur . ' seconds' . "\n";
Error handler
There is also (since v0.4.0) a method on the logger that registers an error handler, that will catch and handle any error (fatals, uncatched exceptions, syntax errors, etc). Remember to call the method as early as possible in your application, as it won't catch any error that occurs before registration.
<?php $logger = new Logger($client, 'php'); // <- Any error that occurs here will NOT be handled. $logger->registerErrorHandler(); error_log('This will NOT be handled either, as error handlers do not catch these messages'); throw new Exception('An uncatched exception that WILL be handled');