cluster28/monolog-config-bundle
Installation:
composer require cluster28/monolog-config-bundle
Ensure your composer.json meets the package’s PHP/Laravel version requirements (check composer.json in the package repo).
Enable the Bundle:
Add the service provider to config/app.php:
'providers' => [
// ...
Cluster28\MonologConfigBundle\MonologConfigBundle::class,
],
Configure Monolog: Publish the default configuration:
php artisan vendor:publish --provider="Cluster28\MonologConfigBundle\MonologConfigBundle" --tag="config"
This generates a config/monolog.php file. Edit it to define your logging channels, handlers, and processors.
First Use Case: Inject the Monolog service into a controller or service:
use Psr\Log\LoggerInterface;
class ExampleController extends Controller
{
public function __construct(private LoggerInterface $logger)
{
$this->logger->info('Application started');
}
}
Verify logs appear in your configured handlers (e.g., storage/logs/laravel.log).
Centralized Logging Configuration:
Use the bundle to define Monolog configurations in a single file (config/monolog.php), avoiding duplication across environments (e.g., dev, staging, prod). Example:
return [
'channels' => [
'single' => [
'handlers' => ['stream'],
],
'daily' => [
'handlers' => ['daily_file'],
'level' => 'debug',
],
],
'handlers' => [
'stream' => [
'type' => 'stream',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily_file' => [
'type' => 'rotating_file',
'path' => storage_path('logs/laravel.log'),
'max_files' => 30,
],
],
];
Environment-Specific Logging:
Override configurations per environment using Laravel’s environment files (e.g., .env). Example:
// config/monolog.php
'channels' => [
'slack' => [
'handlers' => ['slack'],
'level' => env('LOG_LEVEL_SLACK', 'error'),
],
],
Set LOG_LEVEL_SLACK=warning in .env.staging.
Dynamic Handlers:
Register handlers dynamically in a service provider’s boot() method:
public function boot()
{
$this->app['log']->extend('sentry', function ($app) {
return new \Sentry\Monolog\Handler(
new \Sentry\Client($app['config']['sentry.dsn'])
);
});
}
Logging in Middleware: Use the logger in middleware to track HTTP requests:
public function handle($request, Closure $next)
{
$this->logger->info('Incoming request', [
'method' => $request->method(),
'path' => $request->path(),
]);
return $next($request);
}
Logging Exceptions: Override Laravel’s exception handler to log errors with Monolog:
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
public function register()
{
$this->renderable(function (\Throwable $e, $request) {
$this->logger->error('Uncaught exception', [
'exception' => get_class($e),
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
});
}
}
Debugging Workflow:
debug channel in config/monolog.php for development:
'channels' => [
'debug' => [
'handlers' => ['stream'],
'level' => 'debug',
],
],
php artisan tinker to test logging:
\Log::debug('Test debug message');
Deployment Workflow:
'channels' => [
'stack' => [
'handlers' => ['single', 'daily'],
'level' => env('APP_DEBUG') ? 'debug' : 'info',
],
],
php artisan config:clear
php artisan cache:clear
Monitoring Workflow:
monolog channel to forward logs to external services (e.g., Papertrail, Datadog):
'handlers' => [
'syslog' => [
'type' => 'syslog',
'identifier' => 'laravel-app',
'facility' => LOG_USER,
],
],
Laravel Events: Log events in event listeners:
public function handle(UserRegistered $event)
{
$this->logger->info('User registered', [
'user_id' => $event->user->id,
'email' => $event->user->email,
]);
}
Queue Jobs: Log job execution in job classes:
public function handle()
{
$this->logger->info('Job started', ['job' => get_class($this)]);
// Job logic
$this->logger->info('Job completed');
}
Artisan Commands: Log command execution:
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->logger->info('Command executed', [
'command' => $this->getName(),
'input' => $input->all(),
]);
}
Testing: Mock the logger in PHPUnit tests:
$logger = Mockery::mock(\Psr\Log\LoggerInterface::class);
$this->app->instance(\Psr\Log\LoggerInterface::class, $logger);
$logger->shouldReceive('info')->once()->with('Test message');
Configuration Overrides:
php artisan vendor:publish) will use default values, which may not match your needs.config/monolog.php after installation.Handler Conflicts:
stream) can cause unexpected behavior if not properly scoped.app.stream).Environment Mismatches:
.env may not apply if the config file isn’t reloaded.php artisan config:clear
Performance Overhead:
debug level in production) can degrade performance.'handlers' => [
'daily' => [
'type' => 'rotating_file',
'path' => storage_path('logs/laravel.log'),
'max_files' => 30,
],
],
Circular Dependencies:
$this->app->bind(\Psr\Log\LoggerInterface::class, function ($app) {
return new \Monolog\Logger('app', [$app['monolog.handler']]);
});
Alpha-Specific Issues:
composer.json:
"require": {
"cluster28/monolog-config-bundle": "1.0.0-alpha1"
}
Log Not Appearing:
path is correct and writable:
chmod -R 775 storage/logs/
config/monolog.php.Handler Not Triggering:
debug won’t log info messages).$this->logger->error('Test error'); // Always logs if
How can I help you explore Laravel packages today?