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

Laravel Log Viewer Laravel Package

ttbooking/laravel-log-viewer

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation
    composer require ttbooking/laravel-log-viewer
    
  2. Register Provider Add to config/app.php under providers:
    Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class,
    
  3. Publish Config (Optional)
    php artisan vendor:publish --provider="Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider" --tag="config"
    
  4. Route the Controller Add to routes/web.php:
    Route::get('/logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index']);
    

First Use Case

  • Debugging Live Issues: Access /logs to inspect real-time logs without tailing files manually.
  • Quick Error Triage: Filter logs by date, level (error, warning, etc.), or search for specific keywords (e.g., SQLSTATE[42S22]).

Implementation Patterns

Core Workflows

  1. Log Filtering

    • Date Range: Use the UI dropdown to select log files by date (e.g., yesterday, last 7 days).
    • Log Levels: Filter by severity (e.g., error, critical) via the UI toggle buttons.
    • Search: Enter keywords in the search box to narrow down logs (e.g., auth, payment).
  2. Integration with Existing Logs

    • Custom Monolog Handlers: Works seamlessly with default Laravel logging (e.g., single, daily, syslog handlers).
    • Third-Party Loggers: Extend the package to support custom log paths by overriding the LogViewerController's getLogFiles() method.
  3. Security

    • Route Protection: Wrap the route in middleware (e.g., auth, admin) to restrict access:
      Route::get('/logs', [LogViewerController::class, 'index'])->middleware('can:view-logs');
      
    • IP Whitelisting: Add middleware to allow only specific IPs:
      Route::middleware(['ip', 'throttle:60,1'])->group(function () {
          Route::get('/logs', [LogViewerController::class, 'index']);
      });
      
  4. Automated Log Review

    • Scheduled Checks: Use Laravel’s schedule to trigger log reviews (e.g., daily error reports):
      // app/Console/Kernel.php
      protected function schedule(Schedule $schedule)
      {
          $schedule->command('log:review')->dailyAt('9:00');
      }
      
    • Slack/Email Alerts: Parse logs via the API and send alerts for critical issues:
      $logs = $this->logViewer->getLogs(['level' => 'error']);
      if ($logs->contains('payment_failed')) {
          Notifiable::send(new PaymentFailedAlert($logs));
      }
      

Advanced Patterns

  • Log Anonymization Override the formatLogLine() method in a custom controller to redact sensitive data:
    public function formatLogLine($line)
    {
        return preg_replace('/"password":"\w+"/', '"password":"[REDACTED]"', $line);
    }
    
  • Log Archiving Use the package to identify stale logs, then archive them programmatically:
    $oldLogs = $this->logViewer->getLogFiles()->where('date', '<', now()->subDays(30));
    foreach ($oldLogs as $file) {
        Storage::disk('logs')->move($file, "archive/{$file}");
    }
    

Gotchas and Tips

Pitfalls

  1. Log Rotation Conflicts

    • Issue: If logs are rotated/archived by a cron job (e.g., logrotate), the viewer may show incomplete or missing logs.
    • Fix: Ensure the package’s log_file_glob_pattern in config/laravel-log-viewer.php matches your rotation scheme (default: storage/logs/laravel-*.log).
      'log_file_glob_pattern' => storage_path('logs/laravel-*.log'),
      
  2. Large Log Files

    • Issue: Loading multi-GB log files can crash PHP or time out.
    • Fix:
      • Limit the number of lines displayed via config:
        'max_lines' => 10000,
        
      • Use tail functionality to load logs incrementally (requires custom controller extension).
  3. Permission Denied

    • Issue: Access denied errors if the web server user (e.g., www-data) lacks read permissions.
    • Fix: Grant permissions to the log directory:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/
      
  4. Lumen Compatibility

    • Issue: Lumen’s minimal setup may require manual route registration.
    • Fix: Register the controller manually in bootstrap/app.php:
      $app->router->get('/logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index']);
      

Debugging Tips

  • Log File Paths: Verify the correct paths are being scanned by dumping the getLogFiles() output:
    dd($this->logViewer->getLogFiles());
    
  • Custom Log Handlers: If using non-standard log paths (e.g., syslog), extend the controller:
    public function getLogFiles()
    {
        return [sys_get_temp_dir() . '/custom.log'];
    }
    
  • Performance: For slow responses, check if the log_file_glob_pattern is matching too many files. Use a more specific pattern (e.g., laravel-*.log instead of *.log).

Extension Points

  1. Custom Log Formatting Override the formatLogLine() method to add syntax highlighting or context:
    public function formatLogLine($line)
    {
        return highlight_string($line, true);
    }
    
  2. API Endpoint Expose logs via an API for external tools:
    // routes/api.php
    Route::get('/api/logs', [LogViewerController::class, 'apiGetLogs']);
    
    Extend the controller to support JSON responses:
    public function apiGetLogs(Request $request)
    {
        return response()->json($this->logViewer->getLogs($request->all()));
    }
    
  3. Database-Backed Logs For database log handlers, create a custom adapter to query the failed_jobs or logs tables directly.

Config Quirks

  • Timezone Handling: Log timestamps may appear in UTC. Force local timezone in the config:
    'timezone' => 'America/New_York',
    
  • Log Level Mapping: Customize the severity labels (e.g., map debug to info):
    'log_levels' => [
        'debug' => 'Info',
        'info' => 'Info',
        'notice' => 'Notice',
        // ...
    ],
    
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