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

Command Logger Bundle Laravel Package

ayaou/command-logger-bundle

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Symfony/Laravel Compatibility: The bundle is designed for Symfony (not Laravel), but its core functionality—logging console command executions—can be adapted for Laravel via a custom wrapper or middleware. Laravel’s Artisan CLI shares similar command execution patterns, making this feasible.
  • Database Dependency: Relies on Doctrine ORM (Symfony) for storing logs in a command_log table. Laravel’s Eloquent or Query Builder could replace this with minimal effort.
  • Event-Driven Hooks: Leverages Symfony’s event system (e.g., console.command events). Laravel’s Artisan::starting/Artisan::finished events or middleware could replicate this behavior.
  • Attribute-Based Logging: Uses Symfony’s #[CommandLogger] attribute. Laravel could use traits, annotations (via doctrine/annotations), or custom attributes (PHP 8.0+) for equivalent functionality.

Integration Feasibility

  • Low Code Overhead: Minimal changes required to adapt for Laravel:
    • Replace Doctrine with Eloquent for log storage.
    • Replace Symfony events with Laravel’s Artisan hooks or middleware.
    • Port the CommandLogger attribute to Laravel’s attribute system or annotations.
  • Configuration Flexibility: Supports both attribute-based and YAML-based command whitelisting. Laravel’s config/ system can mirror this.
  • Existing CLI Tools: The command-logger:show and command-logger:purge commands can be replicated as Laravel Artisan commands.

Technical Risk

  • Symfony-Specific Abstractions: Risk of tight coupling to Symfony’s Console component (e.g., Command class, event system). Mitigation: Abstract dependencies behind interfaces.
  • Database Schema Mismatch: Laravel’s Eloquent may require adjustments to the command_log table schema (e.g., timestamps, JSON fields).
  • Performance Impact: Logging every command adds overhead. Risk of slowing down CI/CD pipelines or long-running commands. Mitigation: Disable in production or use async logging.
  • Wildcard Command Matching: Symfony’s wildcard support (make:*) may not translate directly to Laravel’s command resolution. Custom logic may be needed.

Key Questions

  1. Prioritization:
    • Is observability of command executions a critical need (e.g., debugging, auditing) or nice-to-have?
    • Will this replace existing logging (e.g., Monolog) or supplement it?
  2. Scope:
    • Should all commands be logged, or only specific ones (e.g., custom commands, migrations)?
    • Are there performance-sensitive commands (e.g., migrate, queue:work) that should be excluded?
  3. Storage:
    • Should logs persist in a database, or could they be written to a file (e.g., JSON) or external service (e.g., ELK)?
  4. Retention:
    • What is the acceptable log retention period (default: 100 days)?
  5. Alternatives:
    • Could existing tools (e.g., Laravel’s built-in logging, third-party packages like spatie/laravel-command) meet the need with less effort?
  6. Testing:
    • How will logs be validated in CI/CD (e.g., asserting command success/failure)?
    • Should logs be exported for post-mortems or compliance?

Integration Approach

Stack Fit

  • Laravel Compatibility:
    • Artisan CLI: Directly compatible with Laravel’s command structure.
    • Database: Replace Doctrine with Eloquent (supports JSON fields for arguments).
    • Events: Use Laravel’s Artisan::starting/Artisan::finished events or middleware to intercept commands.
    • Attributes: Implement custom attributes (PHP 8.0+) or annotations (via doctrine/annotations) for command logging.
  • Symfony vs. Laravel Differences:
    Feature Symfony Implementation Laravel Adaptation
    Command Registration #[AsCommand] attribute Artisan::command() or #[ConsoleCommand]
    Event System console.command events Artisan::starting/finished events
    Configuration YAML (config/packages/) PHP (config/command_logger.php)
    Database ORM Doctrine Eloquent

Migration Path

  1. Phase 1: Proof of Concept (1–2 weeks)
    • Create a minimal Laravel wrapper for the bundle’s core logic:
      • Intercept Artisan commands using middleware or events.
      • Store logs in a custom Eloquent model (CommandLog).
      • Test with 2–3 critical commands (e.g., migrate, queue:work).
    • Example middleware:
      public function handle($request, Closure $next) {
          if ($request->isConsole()) {
              $start = microtime(true);
              try {
                  $response = $next($request);
                  $this->logCommand($request->command, $response->exitCode, null, $start);
              } catch (\Exception $e) {
                  $this->logCommand($request->command, 1, $e->getMessage(), $start);
                  throw $e;
              }
          }
          return $next($request);
      }
      
  2. Phase 2: Full Integration (2–3 weeks)
    • Replace Doctrine with Eloquent:
      // app/Models/CommandLog.php
      class CommandLog extends Model {
          protected $casts = ['arguments' => 'json'];
      }
      
    • Port configuration to Laravel’s config/ system.
    • Implement attribute-based logging (e.g., #[LogCommand]).
    • Add CLI commands (command-logger:show, command-logger:purge) as Artisan commands.
  3. Phase 3: Optimization (1 week)
    • Add async logging (e.g., queue jobs) for performance-sensitive commands.
    • Implement wildcard command matching for Laravel’s command resolution.
    • Add filters to the show command (e.g., --error, --success).

Compatibility

  • Laravel Versions: Supports PHP 8.1+ (Laravel 9+). Tested up to Symfony 8 (Laravel’s future versions may require adjustments).
  • Third-Party Commands: Wildcard support (make:*) may need custom logic for Laravel’s command loader.
  • Existing Logging: Can coexist with Monolog but may duplicate logs. Consider deduplication or routing logs to a separate channel.

Sequencing

  1. Prerequisites:
    • Ensure Laravel’s database is compatible with the command_log schema.
    • Resolve any conflicts with existing Artisan events/middleware.
  2. Core Implementation:
    • Start with middleware-based logging (lowest risk).
    • Gradually replace with attribute-based logging for new commands.
  3. CLI Tools:
    • Implement command-logger:show and command-logger:purge last (depends on storage layer).
  4. Testing:
    • Validate logs for critical commands in staging.
    • Performance-test with high-command-volume environments (e.g., CI).

Operational Impact

Maintenance

  • Dependencies:
    • Laravel-specific: Eloquent, Artisan events, custom attributes.
    • Symfony-specific: Doctrine, Symfony events (minimal if abstracted).
    • Risk: Future Laravel/Symfony updates may require bundle adjustments (e.g., Symfony 8 support in v1.6.0).
  • Configuration:
    • Centralized in config/command_logger.php (e.g., enabled, purge threshold, command whitelist).
    • Tooling: Use Laravel’s config:cache to optimize performance.
  • Updates:
    • Monitor for upstream Symfony changes (e.g., event system deprecations).
    • Plan for annual bundle updates (e.g., Symfony 9/Laravel 11 compatibility).

Support

  • Debugging:
    • Logs provide a audit trail for command failures (e.g., exitCode: 1, errorMessage).
    • Example Use Case: Debugging a silent migrate failure in CI.
  • Troubleshooting:
    • command-logger:show --error helps identify flaky commands.
    • Limitations: No support for nested command execution (e.g., commands called from other commands).
  • Documentation:
    • Update Laravel-specific docs for:
      • Attribute usage (#[LogCommand]).
      • CLI command syntax (e.g., php artisan command-logger:show --limit=20).
    • Example: Add a README.md section for Laravel integration.

Scaling

  • Performance:
    • Synchronous Logging: Adds ~1–5ms per command (negligible for most use cases).
    • Async Logging: Use Laravel queues to offload logging for long-running commands (e.g., queue:work).
    • Database Load: Index startTime and commandName for fast queries in command-logger:show.
  • Log Volume:
    • Retention: Default 100-day purge threshold may need adjustment (e.g., 30 days for high-volume systems).
    • Archiving: Consider exporting logs
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.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony
spatie/flare-daemon-runtime