Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Loggingbundle Laravel Package

alpixel/loggingbundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:
    composer require alpixel/loggingbundle
    
  2. Register the Bundle in app/AppKernel.php:
    new Alpixel\Bundle\LoggingBundle\AlpixelLoggingBundle(),
    new Symfony\Bundle\MonologBundle\MonologBundle(),
    
  3. Configure Slack in 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
    
  4. Remove default Monolog handlers from config_{prod,dev}.yml to avoid conflicts.

First Use Case

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.


Implementation Patterns

Core Workflows

  1. Slack Integration:

    • Automatic: Logs marked as critical, error, or alert (if configured) trigger Slack notifications.
    • Manual: Use logger->alert() for high-priority messages (if extended via processors).
  2. Contextual Logging: Add metadata to logs for debugging:

    $this->logger->error('Failed to save entity', [
        'entity' => $entity,
        'user'   => $this->getUser()->getId(),
        'trace'  => $e->getTraceAsString()
    ]);
    
  3. Environment-Specific Behavior:

    • Use debug: true in config_dev.yml to bypass Slack in development.
    • Override bot_name or channel per environment:
      alpixel_logging:
          slack:
              channel: "#dev-alerts"  # config_dev.yml
      

Integration Tips

  • Sonata Admin: Extend AlpixelLoggingBundle to log admin actions:
    // In a Sonata event listener
    $logger->info('Admin action', ['action' => 'delete', 'entity' => $entityClass]);
    
  • Custom Processors: Add a processor key in config to transform logs:
    alpixel_logging:
        slack:
            processors:
                - Alpixel\Bundle\LoggingBundle\Processor\CustomProcessor
    
  • Rate Limiting: Use Monolog’s fingers_crossed handler to throttle Slack alerts:
    monolog:
        handlers:
            slack:
                type:         fingers_crossed
                action_level: error
                handler:     alpixel_slack
    

Gotchas and Tips

Pitfalls

  1. Double Logging:

    • If MonologBundle handlers (e.g., stream, console) remain in config_{prod,dev}.yml, logs will duplicate.
    • Fix: Remove all Monolog handlers except alpixel_slack in production.
  2. Slack Token Exposure:

    • Hardcoding tokens in config.yml violates security best practices.
    • Fix: Use environment variables (%env(SLACK_TOKEN)%) or .env files.
  3. Deprecated Symfony Version:

    • The bundle supports Symfony 2.8–3.0 but lacks PHP 8+ compatibility.
    • Workaround: Fork and update dependencies if using newer PHP/Symfony.
  4. Debug Mode Quirks:

    • debug: true disables Slack but may still log to other handlers if misconfigured.
    • Tip: Verify no other handlers are active in config_dev.yml.

Debugging

  • Check Slack Webhook:
    • Test the token/channel with curl:
      curl -X POST -H 'Content-type: application/json' \
      --data '{"text":"Test message"}' \
      https://hooks.slack.com/services/TOKEN
      
  • Log Levels:
    • Default levels (critical, error) may not trigger Slack. Override in config:
      alpixel_logging:
          slack:
              levels: [error, warning, info]  # Customize as needed
      
  • Processor Debugging:
    • Add debug: true and check Slack for raw log data to verify transformations.

Extension Points

  1. Custom Handlers: Extend Alpixel\Bundle\LoggingBundle\Handler\SlackHandler to add features like:

    • Attachments (e.g., screenshots for error logs).
    • Interactive messages (e.g., "Resolve" buttons).
  2. 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';
    
  3. Async Logging: Use async handlers to prevent Slack API delays from blocking requests:

    monolog:
        handlers:
            alpixel_slack:
                type:         async
                handler:      alpixel_slack_handler
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log