composer require alpixel/loggingbundle
app/AppKernel.php:
new Alpixel\Bundle\LoggingBundle\AlpixelLoggingBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
config.yml:
alpixel_logging:
slack:
token: "%env(SLACK_TOKEN)%" # Use env vars for security
bot_name: "Error Bot"
channel: "#alerts"
debug: "%kernel.debug%" # Auto-enable in dev
config_{prod,dev}.yml to avoid conflicts.Log a critical error to Slack:
use Psr\Log\LoggerInterface;
class MyController
{
public function __construct(private LoggerInterface $logger)
{
}
public function handleError()
{
$this->logger->critical('Database connection failed', [
'exception' => new \RuntimeException('Connection timeout'),
'context' => ['user_id' => 123]
]);
}
}
Expected: Slack message formatted with bot_name, timestamp, and error details.
Slack Integration:
critical, error, or alert (if configured) trigger Slack notifications.logger->alert() for high-priority messages (if extended via processors).Contextual Logging: Add metadata to logs for debugging:
$this->logger->error('Failed to save entity', [
'entity' => $entity,
'user' => $this->getUser()->getId(),
'trace' => $e->getTraceAsString()
]);
Environment-Specific Behavior:
debug: true in config_dev.yml to bypass Slack in development.bot_name or channel per environment:
alpixel_logging:
slack:
channel: "#dev-alerts" # config_dev.yml
AlpixelLoggingBundle to log admin actions:
// In a Sonata event listener
$logger->info('Admin action', ['action' => 'delete', 'entity' => $entityClass]);
processor key in config to transform logs:
alpixel_logging:
slack:
processors:
- Alpixel\Bundle\LoggingBundle\Processor\CustomProcessor
fingers_crossed handler to throttle Slack alerts:
monolog:
handlers:
slack:
type: fingers_crossed
action_level: error
handler: alpixel_slack
Double Logging:
MonologBundle handlers (e.g., stream, console) remain in config_{prod,dev}.yml, logs will duplicate.alpixel_slack in production.Slack Token Exposure:
config.yml violates security best practices.%env(SLACK_TOKEN)%) or .env files.Deprecated Symfony Version:
Debug Mode Quirks:
debug: true disables Slack but may still log to other handlers if misconfigured.config_dev.yml.curl:
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"Test message"}' \
https://hooks.slack.com/services/TOKEN
critical, error) may not trigger Slack. Override in config:
alpixel_logging:
slack:
levels: [error, warning, info] # Customize as needed
debug: true and check Slack for raw log data to verify transformations.Custom Handlers:
Extend Alpixel\Bundle\LoggingBundle\Handler\SlackHandler to add features like:
error logs).Dynamic Channels: Route logs to different Slack channels based on context:
// In a processor
$record['extra']['channel'] = $record['context']['severity'] === 'security'
? '#security-alerts'
: '#general-alerts';
Async Logging:
Use async handlers to prevent Slack API delays from blocking requests:
monolog:
handlers:
alpixel_slack:
type: async
handler: alpixel_slack_handler
How can I help you explore Laravel packages today?