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

rap2hpoutre/laravel-log-viewer

Lightweight Laravel log viewer for Laravel 12/13. Install via Composer and add a single route to LogViewerController to browse app logs in the browser. No public assets or vendor routes; works with or without log rotation. View and config are publishable.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:
    composer require rap2hpoutre/laravel-log-viewer
    
  2. Register Service Provider (Laravel 12/13 auto-discovers, but add to config/app.php if needed):
    Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class,
    
  3. Add Route (e.g., in routes/web.php):
    Route::get('logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index'])->middleware('auth');
    
  4. Access Logs: Visit /logs in your browser.

First Use Case

  • Debugging a Staging Issue: Quickly inspect Laravel logs (e.g., laravel.log, daily rotated logs) without CLI access.
  • Development Workflow: Replace tail -f storage/logs/laravel.log with a searchable UI.

Where to Look First

  • Default View: /resources/views/vendor/laravel-log-viewer/log.blade.php (published via vendor:publish).
  • Configuration: /config/logviewer.php (customize log paths, UI settings).
  • Controller: \Rap2hpoutre\LaravelLogViewer\LogViewerController (extend for custom logic).

Implementation Patterns

Core Workflows

  1. Basic Log Viewing:

    • Navigate to /logs to see all Laravel log files (default: storage/logs/laravel.log and daily rotated logs).
    • Click log files to view their contents with syntax highlighting.
  2. Searching Logs:

    • Use the search bar to filter logs by keywords (case-insensitive).
    • Supports partial matches (e.g., type:error or user:123).
  3. Custom Log Paths:

    • Override default log paths in config/logviewer.php:
      'log_files' => [
          storage_path('logs/custom.log'),
          storage_path('logs/app.log'),
      ],
      
  4. Middleware Integration:

    • Restrict access to authenticated/admin users:
      Route::get('logs', [LogViewerController::class, 'index'])->middleware(['auth', 'admin']);
      

Integration Tips

  • Pair with Laravel Debugbar: Combine with barryvdh/laravel-debugbar for a unified debugging dashboard.
  • Environment-Specific Routes: Exclude production by checking the environment:
    if (app()->environment('local', 'staging')) {
        Route::get('logs', [LogViewerController::class, 'index']);
    }
    
  • Log Redaction: Extend the controller to redact sensitive data (e.g., passwords, tokens) before rendering:
    public function index()
    {
        $logs = $this->getLogs();
        $redactedLogs = collect($logs)->map(fn ($log) => preg_replace('/password=[^&]+/', 'password=****', $log));
        return view('vendor.laravel-log-viewer.log', ['logs' => $redactedLogs]);
    }
    
  • Custom Views: Publish and modify the Blade template to add features like:
    • Collapsible log entries.
    • Copy-to-clipboard buttons.
    • Log level filtering (e.g., error, warning).

Advanced Patterns

  • Dynamic Log Paths: Fetch log paths dynamically (e.g., from a config file or database):
    'log_files' => config('logging.custom_paths'),
    
  • Log Export: Add an endpoint to export logs as JSON/CSV:
    Route::get('logs/export', [LogViewerController::class, 'export']);
    
    public function export()
    {
        return response()->json($this->getLogs());
    }
    
  • Webhook Alerts: Integrate with Slack/Teams by parsing logs for critical errors and triggering alerts.

Gotchas and Tips

Pitfalls

  1. Permission Errors:

    • Issue: Log files may be unreadable due to incorrect permissions.
    • Fix: Ensure storage/logs/ is writable:
      chmod -R 755 storage/logs/
      
    • Debug: Check for InvalidArgumentException in logs; verify file paths in config/logviewer.php.
  2. Config Cache Issues:

    • Issue: InvalidArgumentException in FileViewFinder.php after publishing views.
    • Fix: Clear config cache:
      php artisan config:clear
      
  3. Log Rotation Gaps:

    • Issue: Daily rotated logs (e.g., laravel-2024-05-10.log) may not appear if the package isn’t updated to v2.0.0+.
    • Fix: Update to the latest version or manually specify log paths.
  4. Sensitive Data Exposure:

    • Issue: Logs may contain passwords, API keys, or PII.
    • Fix:
      • Restrict route access (e.g., auth, admin middleware).
      • Redact logs server-side (see Integration Tips).
      • Exclude production from log viewer usage.
  5. Large Log Files:

    • Issue: Slow rendering for files >100MB.
    • Fix:
      • Implement client-side pagination (e.g., load 100 lines at a time).
      • Use tail to show only recent logs by default.

Debugging Tips

  • Check Log Paths: Verify config/logviewer.php points to the correct log files. Default:
    'log_files' => [
        storage_path('logs/laravel.log'),
        storage_path('logs/laravel-*.log'),
    ],
    
  • View Raw Logs: Temporarily modify the controller to output raw logs for debugging:
    public function index()
    {
        dd($this->getLogs()); // Debug log contents
    }
    
  • Disable Caching: If views aren’t updating, disable Blade caching:
    php artisan view:clear
    

Extension Points

  1. Custom Log Levels: Extend the LogViewerController to filter logs by level (e.g., error, debug):

    public function index()
    {
        $logs = $this->getLogs()->filter(fn ($log) => str_contains($log, '[ERROR]'));
        return view('...', ['logs' => $logs]);
    }
    
  2. Database-Backed Logs: Override the controller to fetch logs from a database (e.g., log_entries table):

    public function getLogs()
    {
        return DB::table('log_entries')->get()->pluck('message');
    }
    
  3. Real-Time Updates: Use Laravel Echo/Pusher to stream new logs in real-time (requires custom implementation).

  4. Multi-Tenant Logs: Dynamically set log paths based on the current tenant:

    'log_files' => [storage_path("logs/tenant-{$tenantId}.log")],
    

Configuration Quirks

  • Log File Patterns: Use glob patterns for rotated logs (e.g., laravel-*.log). Avoid wildcards in non-standard paths.
  • Case Sensitivity: Search is case-insensitive, but log levels (e.g., ERROR vs error) may require normalization.
  • Memory Limits: Large log files may hit PHP’s memory_limit. Increase if needed:
    ini_set('memory_limit', '512M');
    

Performance Tips

  • Lazy Loading: Implement infinite scroll to load logs in chunks (requires JavaScript).
  • Pre-Process Logs: Use a cron job to compress old logs and exclude them from the viewer:
    find storage/logs/ -name "laravel-*.log" -mtime +30 -exec gzip {} \;
    
  • CDN for Assets: If customizing the view, host static assets (e.g., CSS) on a CDN to reduce load times.
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony