Installation:
composer require fluent/logger:^1.0.0
Add to composer.json under require:
"fluent/logger": "^1.0.0"
Basic Usage:
use Fluent\Logger\FluentLogger;
$logger = new FluentLogger('localhost', 24224); // Host, Port
$logger->post('app.log', ['message' => 'Test log entry']);
First Use Case:
Replace error_log() or Monolog in a Laravel app with Fluentd integration:
$logger = new FluentLogger(config('fluent.host'), config('fluent.port'));
$logger->post('laravel.errors', ['exception' => $e->getMessage()]);
Structured Logging:
$logger->post('user.activity', [
'user_id' => 123,
'action' => 'login',
'ip' => $_SERVER['REMOTE_ADDR'],
'timestamp' => now()->toIso8601String()
]);
Error Handling Integration:
// Register error handler (v1.0.1+)
$logger->registerErrorHandler();
// Unregister when needed
$logger->unregisterErrorHandler();
Laravel Service Provider:
// config/fluent.php
return [
'host' => env('FLUENT_HOST', 'localhost'),
'port' => env('FLUENT_PORT', 24224),
'tag_prefix' => 'laravel.'
];
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('fluent.logger', function () {
return new FluentLogger(
config('fluent.host'),
config('fluent.port')
);
});
}
Tag Prefixes:
Use a consistent prefix (e.g., laravel.) for all logs to group them in Fluentd:
$logger->post('laravel.requests', ['path' => $request->path()]);
Async Logging: Offload logging to a background process (recommended per README):
PHP App → Local Fluentd Proxy → Central Fluentd Aggregator
No Buffering:
Deprecated Classes:
FluentLogger remains in v1.x. Other loggers (e.g., FluentHandler) are removed.PHP Threading Limitation:
Fluentd Version:
Error Handler Quirks:
registerErrorHandler() captures all errors (including notices). Use sparingly in production.Connection Issues:
telnet localhost 24224.<match **>
@type stdout
</match>
Log Format:
$logger->post('test', json_decode('{"key":"value"}', true));
Performance:
microtime():
$start = microtime(true);
$logger->post('test', ['data' => 'x']);
error_log(microtime(true) - $start); // Should be < 100ms
Custom Transport:
Override FluentLogger::send() to add retries or compression:
class CustomFluentLogger extends FluentLogger {
protected function send($data) {
// Add retry logic here
parent::send($data);
}
}
Log Level Filtering:
Extend to support levels (e.g., debug, error):
$logger->post('debug.app', ['message' => 'Debug info'], 'debug');
Laravel Log Channel: Create a custom channel (Laravel 5.5+):
// app/Providers/AppServiceProvider.php
Log::extend('fluent', function ($app) {
return new class {
protected $logger;
public function __construct() {
$this->logger = new FluentLogger(
config('fluent.host'),
config('fluent.port')
);
}
public function log($level, $message, array $context = []) {
$this->logger->post('laravel.log', $context);
}
};
});
Tag Generation: Dynamically generate tags based on context:
$logger->post("app.{$request->path()}", ['data' => $payload]);
Environment-Specific Hosts:
$host = env('FLUENT_HOST', env('APP_ENV') === 'local' ? 'localhost' : 'fluent.prod');
Fluentd Proxy Setup:
Use td-agent (Fluentd for Debian/Ubuntu) with:
# /etc/td-agent/td-agent.conf
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<match **>
@type forward
<server>
host fluent.prod
port 24224
</server>
</match>
Log Retention: Configure Fluentd to manage disk space:
<match **>
@type file
path /var/log/fluent/app.log
<storage>
@type local
path /var/log/fluent
timekey_wait 10s
timekey_use_utc true
timekey_format %Y%m%d
</storage>
</match>
How can I help you explore Laravel packages today?