Weave Code
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

Auditlog Bundle Laravel Package

bbit/auditlog-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require bbit/auditlog-bundle:0.1
    

    Ensure BBIT\AuditLogBundle\BBITAuditLogBundle() is registered in AppKernel.php.

  2. Database Migration: The bundle requires a audit_log table. Manually create it or use a migration:

    // src/Migrations/YYYY_MM_DD_create_audit_log_table.php
    Schema::create('audit_log', function (Blueprint $table) {
        $table->increments('id');
        $table->string('type');
        $table->string('channel');
        $table->text('message');
        $table->timestamps();
    });
    
  3. First Log Entry: Inject the service in a controller or command:

    use BBIT\AuditLogBundle\Service\AuditLogService;
    
    class ExampleController extends Controller
    {
        public function __construct(AuditLogService $logger)
        {
            $this->logger = $logger;
        }
    
        public function index()
        {
            $this->logger->log('security', 'auth', 'User logged in');
        }
    }
    

Implementation Patterns

Core Workflows

  1. Logging Events: Use log($type, $channel, $message) for structured entries. Example:

    $this->logger->log('admin', 'dashboard', 'User deleted record #123');
    
  2. Integration with Symfony Events: Attach the logger to event listeners:

    // src/EventListener/AuditListener.php
    class AuditListener implements EventSubscriberInterface
    {
        public function __construct(AuditLogService $logger) {}
    
        public function onKernelRequest(GetResponseEvent $event)
        {
            $this->logger->log('system', 'request', 'Incoming request: ' . $event->getRequest()->getPathInfo());
        }
    }
    
  3. Batch Logging: For performance, batch logs in commands:

    // src/Command/ProcessLogsCommand.php
    class ProcessLogsCommand extends Command
    {
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $logs = [];
            foreach ($items as $item) {
                $logs[] = ['type' => 'data', 'channel' => 'import', 'message' => $item->logMessage()];
            }
            // Bulk insert (requires custom DB logic; bundle lacks native support)
        }
    }
    

Advanced Patterns

  • Dynamic Channels: Use closures for dynamic channel/message generation:

    $this->logger->log('user', function ($log) {
        $log->setChannel('user_' . $user->id);
        $log->setMessage('Profile updated: ' . $user->email);
    });
    
  • Conditional Logging: Wrap logs in checks to avoid clutter:

    if ($this->isDebug()) {
        $this->logger->log('debug', 'controller', 'Rendered view: ' . $viewName);
    }
    

Gotchas and Tips

Pitfalls

  1. No Native Query Builder: The bundle lacks a fluent query interface. Use raw queries or a custom repository:

    $logs = DB::table('audit_log')
        ->where('type', 'security')
        ->where('created_at', '>', now()->subDays(7))
        ->get();
    
  2. Missing Configuration: The bundle has no config.yml support. Hardcode defaults or extend the service:

    # No config file exists; override via DI if needed.
    
  3. No Soft Deletes: The audit_log table lacks a deleted_at column. Implement manually if needed.

  4. Performance: Avoid logging in loops without batching. For high-volume logs, consider:

    DB::beginTransaction();
    foreach ($items as $item) {
        DB::table('audit_log')->insert([...]);
    }
    DB::commit();
    

Debugging Tips

  • Check Table Structure: Verify the audit_log table matches the expected schema (e.g., type, channel, message columns).

  • Log Service Availability: Ensure the service is autowired correctly. Test with:

    $this->logger->log('test', 'init', 'Service test');
    
  • Query Logs: Enable Doctrine logging to debug queries:

    // config/packages/dev/doctrine.yaml
    doctrine:
        dbal:
            logging: true
            profiling: true
    

Extension Points

  1. Custom Log Model: Extend the bundle’s AuditLog entity or create a proxy:

    class CustomAuditLog extends \BBIT\AuditLogBundle\Entity\AuditLog
    {
        public function setAdditionalData(array $data) { ... }
    }
    
  2. Event Dispatching: Trigger Symfony events after logging:

    // Override the service to dispatch events
    $event = new AuditLoggedEvent($log);
    $this->dispatcher->dispatch($event, AuditLoggedEvent::NAME);
    
  3. Async Logging: Use a queue (e.g., Symfony Messenger) for non-critical logs:

    $message = new AuditLogMessage('type', 'channel', 'message');
    $this->messageBus->dispatch($message);
    
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.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php