Install the Package
Add to composer.json:
"require": {
"deamon/logger-extra-bundle": "^7.0"
}
Run composer update.
Configure Monolog
Ensure your config/logging.php (Laravel) or config/packages/monolog.yml (Symfony) has a stream handler:
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'deprecations'],
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
],
Add Bundle Configuration
Create config/deamon_logger_extra.php (Laravel) or config/packages/deamon_logger_extra.yml (Symfony):
return [
'application' => [
'name' => 'my-laravel-app',
'version' => '1.0.0',
],
'handlers' => ['single'], // Match your Monolog handler name
'config' => [
'display' => [
'user' => true,
'client_ip' => true,
],
],
];
First Use Case Log a request with context:
\Log::debug('User action', [
'custom_data' => 'test',
]);
Expected log output:
{
"level": "debug",
"message": "User action",
"context": {
"custom_data": "test",
"user": {"id": 1, "name": "john"},
"client_ip": "192.168.1.1",
"application": {"name": "my-laravel-app", "version": "1.0.0"}
}
}
Context Injection
user, client_ip, route) into every log record for specified handlers.use Deamon\LoggerExtraBundle\Processor\ExtraContextProcessor;
use Monolog\Processor\ProcessorInterface;
class CustomContextProcessor implements ProcessorInterface {
public function __invoke(array $record): array {
$record['extra']['custom_metric'] = 'value';
return $record;
}
}
// Register in AppServiceProvider
\Monolog\Logger::pushProcessor(new CustomContextProcessor());
Handler-Specific Configuration
single, stack) via the handlers config array.single handler:
'handlers' => ['single'],
Conditional Display
display config:
'config' => [
'display' => [
'user' => env('APP_ENV') !== 'production',
'client_ip' => true,
],
],
Debugging Workflow
config/deamon_logger_extra.php:
'config' => [
'display' => [
'env' => true,
'locale' => true,
'application_name' => true,
'url' => true,
'route' => true,
'user_agent' => true,
'accept_encoding' => true,
'client_ip' => true,
'user' => true,
'global_channel' => true,
],
],
Performance Optimization
'config' => [
'display' => [
'user' => false, // Disable if not needed
'client_ip' => true,
],
],
Integration with Laravel Logging
// Log with custom context
\Log::info('Order processed', [
'order_id' => 123,
'user_id' => auth()->id(),
]);
Avoid Symfony Dependencies
composer.json to prevent conflicts:
"require": {
"deamon/logger-extra-bundle": "^7.0",
"symfony/security-core": null, // Explicitly remove
},
Custom User Class
UserInterface for Laravel’s App\Models\User:
'config' => [
'user_class' => \App\Models\User::class,
'user_methods' => [
'user_name' => 'name', // Use 'name' instead of 'getUsername'
],
],
Log Channel Routing
'channels' => [
'debug' => [
'driver' => 'single',
'path' => storage_path('logs/debug.log'),
'level' => 'debug',
],
'error' => [
'driver' => 'single',
'path' => storage_path('logs/error.log'),
'level' => 'error',
],
],
debug handler:
'handlers' => ['debug'],
Symfony Dependency Conflicts
symfony/security-core), which may conflict with Laravel.use Deamon\LoggerExtraBundle\Processor\ExtraContextProcessor;
\Monolog\Logger::pushProcessor(new ExtraContextProcessor());
Missing Context Fields
user or client_ip may appear as null if not properly initialized.user_class and user_methods are correctly configured.Authenticate for Laravel) populates the request/user data before logging.Handler Mismatch
handlers config doesn’t match the Monolog handler.single vs. stack).Performance Overhead
debug channel).Deprecation Warnings
Log Context Inspection
'config' => [
'display' => [
'env' => true,
'locale' => true,
'application_name' => true,
'url' => true,
'route' => true,
'user_agent' => true,
'accept_encoding' => true,
'client_ip' => true,
'user' => true,
'global_channel' => true,
],
],
Processor Debugging
\Monolog\Logger::pushProcessor(function ($record) {
\Log::debug('Raw record before context:', $record);
return $record;
});
Handler-Specific Logging
\Log::debug('Test debug log', [], ['channel' => 'debug']);
\Log::error('Test error log', [], ['channel' => 'error']);
Handler Name Sensitivity
handlers config is case-sensitive and must match the Monolog handler name exactly (e.g., single vs. Single).User Class Requirements
user_class must implement Laravel’s Illuminate\Contracts\Auth\Authenticatable or Symfony’s UserInterface.How can I help you explore Laravel packages today?