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

Logviewer Bundle Laravel Package

cyve/logviewer-bundle

Symfony bundle that adds a lightweight log viewer to your app. Install via Composer, enable the bundle, and import its routes. Super admins (ROLE_SUPER_ADMIN) can browse application logs at /logs to quickly inspect recent entries and errors.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Run:

    composer require cyve/logviewer-bundle
    

    Ensure the bundle is enabled in config/bundles.php:

    Cyve\LogViewerBundle\CyveLogViewerBundle::class => ['all' => true],
    
  2. Routing Import the routes in config/routes/cyve_log_viewer.yaml:

    cyve_log_viewer:
        resource: "@CyveLogViewerBundle/Resources/config/routing.yaml"
    
  3. First Access

    • Navigate to /logs in your browser.
    • Ensure your user has the ROLE_SUPER_ADMIN role (default requirement).

First Use Case: Debugging Live Logs

  • View logs in real-time via the /logs endpoint.
  • Filter logs by level (e.g., ERROR, INFO) or keyword.
  • No additional setup required for basic usage.

Implementation Patterns

Workflow Integration

  1. Development Debugging

    • Use the log viewer during development to inspect application logs without manually tailing files.
    • Example: Debugging a misbehaving API endpoint by filtering logs for POST /api/endpoint.
  2. Production Monitoring (Caution)

    • Enable only in staging/production if absolutely necessary (e.g., for critical incidents).
    • Restrict access via firewall or custom security rules (e.g., IP whitelisting).
  3. Custom Log Levels

    • Extend the bundle to support custom log levels by overriding the LogViewerController and modifying the getLevels() method.
  4. Log File Paths

    • Override the default log paths by configuring the bundle’s log_paths parameter in config/packages/cyve_log_viewer.yaml:
      cyve_log_viewer:
          log_paths: ['%kernel.logs_dir%/app.log', '%kernel.logs_dir%/debug.log']
      

Common Patterns

  • Symfony Event Listeners Integrate with Symfony’s kernel.log event to dynamically inject metadata into logs (e.g., user IDs, request IDs) for easier filtering:

    // src/EventListener/LogListener.php
    public function onKernelLog(GetResponseForExceptionEvent $event) {
        $logEntry = $event->getLogEntry();
        $logEntry->setExtra(['user_id' => $this->getCurrentUserId()]);
    }
    
  • Security Integration Replace the default ROLE_SUPER_ADMIN check with a custom voter or access control list (ACL) for granular permissions:

    # config/packages/security.yaml
    access_control:
        - { path: ^/logs, roles: ROLE_LOG_VIEWER }
    

Gotchas and Tips

Pitfalls

  1. Performance Overhead

    • Issue: Real-time log viewing in production can impact performance if logs are large or frequently written.
    • Fix: Disable the route in production or use a read-only cache (e.g., cyve_log_viewer.cache_enabled: true).
  2. Log Rotation

    • Issue: The bundle may not handle log rotation (e.g., monolog rotating logs). Old logs might appear broken or incomplete.
    • Fix: Ensure monolog is configured to retain logs in a readable format during rotation:
      monolog:
          handlers:
              main:
                  type: rotating_file
                  max_files: 30
                  filename: "%kernel.logs_dir%/%kernel.environment%.log"
      
  3. Missing Logs

    • Issue: Some logs (e.g., those written via file_put_contents) won’t appear in the viewer.
    • Fix: Use Symfony’s LoggerInterface or Monolog for consistency.
  4. CSRF Protection

    • Issue: The /logs endpoint may lack CSRF protection by default.
    • Fix: Add @Security annotations or configure Symfony’s csrf_token middleware to exclude it if safe:
      # config/packages/framework.yaml
      framework:
          csrf_protection:
              enabled: true
              ignore_routes:
                  - cyve_log_viewer_logs
      

Debugging Tips

  1. Log Viewer Not Showing Logs

    • Verify the log_paths configuration matches your actual log file locations.
    • Check file permissions (chmod -R 755 var/logs).
  2. Blank Page or 404

    • Ensure the bundle is enabled in bundles.php.
    • Clear cache:
      php bin/console cache:clear
      
  3. Custom Log Formatting

    • Override the bundle’s twig template (templates/logs/index.html.twig) to customize the log display (e.g., add collapsible sections).

Extension Points

  1. Custom Log Filters Extend the LogFilter service to add dynamic filters (e.g., by request ID):

    // src/Service/CustomLogFilter.php
    public function filter(array $logs, string $query) {
        return array_filter($logs, function ($log) use ($query) {
            return strpos($log['message'], $query) !== false ||
                   isset($log['extra']['request_id']) && strpos($log['extra']['request_id'], $query) !== false;
        });
    }
    
  2. Log Export Add a route to export logs as JSON or CSV by extending the controller:

    // src/Controller/LogExportController.php
    public function export(Request $request) {
        $logs = $this->logViewer->getLogs();
        return new JsonResponse($logs);
    }
    
  3. Real-Time Updates Use Symfony UX Turbo or Mercure to push log updates to the client without polling:

    # config/packages/messenger.yaml
    framework:
        messenger:
            transports:
                async: '%env(MESSENGER_TRANSPORT_DSN)%'
    

Configuration Quirks

  • Environment-Specific Logs The bundle defaults to showing logs for the current environment. Override in config/packages/cyve_log_viewer.yaml:

    cyve_log_viewer:
        environments: [dev, staging] # Show logs only for these environments
    
  • Log Level Whitelisting Restrict visible log levels to ERROR and CRITICAL in production:

    cyve_log_viewer:
        levels: [ERROR, CRITICAL]
    
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.
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
directorytree/privacy-filter-classifier
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