Installation Run:
composer require a5sys/monitor-bundle
Enable Bundles
Add to app/AppKernel.php:
new A5sys\MonitorBundle\MonitorBundle(),
new JMS\TranslationBundle\JMSTranslationBundle(), // Required dependency
Configure Monolog
In app/config/config.yml:
monolog:
channels: ['monitor']
handlers:
monitor:
type: rotating_file
max_files: 60
path: "%kernel.logs_dir%/monitor.log"
channels: ['monitor']
formatter: monitor.monolog.formatter
Exclude monitor from your main log to avoid spam:
monolog:
handlers:
main:
type: rotating_file
channels: [!'monitor']
Route Access
Add to app/config/routing.yml:
monitor_controller:
resource: "@MonitorBundle/Controller/"
type: annotation
prefix: /monitor
First Use Case
Access /monitor in your browser (ensure security is configured). The dashboard will show:
Data Collection
The bundle hooks into Symfony’s event system (e.g., kernel.request, kernel.response) to log:
500, 200).Threshold Configuration
Customize performance thresholds in config.yml:
monitor:
slow_request_threshold: 500 # ms (default)
error_threshold: 100 # requests before alerting
Integration with Existing Logs
Request::get('X-Request-ID') to trace logs across services.monitor.monolog.formatter to include custom metadata (e.g., user IDs, request payloads).Scheduled Reports
Use Symfony’s CronBundle or a queue worker (e.g., Symfony Messenger) to:
monitor.log daily.5xx spikes).Logrotate.Dashboard Customization
Override the default controller (MonitorBundle:Controller\MonitorController) to:
// src/Acme/MonitorBundle/Controller/MonitorController.php
class MonitorController extends \A5sys\MonitorBundle\Controller\MonitorController
{
public function indexAction()
{
$data = parent::indexAction();
$data['custom_metric'] = $this->getCustomMetric();
return $this->render('AcmeMonitorBundle:monitor:index.html.twig', $data);
}
}
API Access Expose metrics via a custom endpoint:
# app/config/routing.yml
monitor_api:
path: /api/monitor/metrics
defaults: { _controller: AcmeMonitorBundle:MonitorApi:metrics }
Log Bloat
max_files: 60) may fill disk space if not monitored.logrotate or increase max_files temporarily during debugging.Performance Overhead
monitor:
enabled_environments: [dev, staging]
Security Risks
/monitor route exposes sensitive data (e.g., slow queries, error stacks).security.yml:
access_control:
- { path: ^/monitor, roles: [ROLE_ADMIN] }
Formatter Conflicts
monitor.monolog.formatter may conflict with other bundles using the same channel.services.yml:
services:
monitor.monolog.formatter:
class: Monolog\Formatter\LineFormatter
arguments: ["[%datetime%] %channel% %level% %message% %context%\n"]
Outdated Dependencies
JMS/TranslationBundle (last updated 2017) and Symfony 2.x.Verify Log Output
Check var/logs/monitor.log for raw data:
tail -f var/logs/monitor.log | grep "slow_request"
Test Event Listeners Trigger a slow request manually:
// In a controller
sleep(1); // Simulate slow request
return new Response('Test');
Verify it appears in the dashboard.
Custom Metrics Extend the bundle’s logger service to add context:
// src/Acme/MonitorBundle/DependencyInjection/CompilerPass.php
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('monitor.logger');
$definition->addMethodCall('addContext', ['custom_key', 'custom_value']);
}
Database Queries
To log slow queries, patch the bundle’s QueryListener:
// Override A5sys\MonitorBundle\EventListener\QueryListener
public function onQuery(QueryEvent $event)
{
if ($event->getStartTime() > 500) { // 500ms threshold
$this->logger->warning('Slow query', ['query' => $event->getSql()]);
}
}
Add Custom Metrics
Inject metrics via the monitor.event service:
// In a controller/service
$this->get('monitor.event')->addMetric('custom_metric', ['value' => 123]);
Alerting System
Hook into the monitor.log handler to send alerts:
# app/config/config.yml
monolog:
handlers:
monitor_alert:
type: fingers_crossed
action_level: error
handler: nested
excluded_404s: ~
channels: ['monitor']
buffer_size: 0
handlers:
slack:
type: stream
path: "php://temp"
formatter: monolog.formatter.slack
streams: ["https://hooks.slack.com/..."]
Export Data
Use the MonitorBundle\Service\MonitorService to fetch raw data:
$monitorService = $this->get('monitor.service');
$slowRequests = $monitorService->getSlowRequests(300); // Threshold in ms
How can I help you explore Laravel packages today?