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

View Laravel and Lumen log files in the browser with a lightweight log viewer. Install via Composer, register the service provider, and add a single route to LogViewerController—no public assets or vendor routes. Works with rotated logs.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Installation: Run composer require rap2hpoutre/laravel-log-viewer in your project root.
  2. Route Setup: Add the route to routes/web.php:
    Route::get('logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index']);
    
  3. First Use: Visit /logs in your browser. The viewer will display all logs from storage/logs/laravel.log (or your configured log path) with syntax highlighting and search functionality.

Key First Steps:

  • Secure the route immediately with middleware (e.g., Route::get('logs', ...)->middleware('auth')).
  • Test in a non-production environment first to verify log visibility and performance.

Implementation Patterns

Daily Workflows

  1. Debugging Errors:

    • Use the search bar to filter for Exception, Error, or specific error messages.
    • Click log entries to expand stack traces and context (e.g., request data, variables).
  2. Monitoring Deployments:

    • Compare logs before/after deployments by checking timestamps.
    • Use the "Clear" button to reset the view after resolving issues.
  3. Collaboration:

    • Share log snippets via the "Copy" button for tickets or Slack.
    • Highlight critical logs with custom CSS (publish the view and modify log.blade.php).

Advanced Patterns

  1. Custom Log Paths:

    • Publish the config: php artisan vendor:publish --provider="Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider".
    • Update config/logviewer.php to specify paths like:
      'paths' => [
          storage_path('logs/custom.log'),
          storage_path('logs/debug.log'),
      ],
      
  2. Log Filtering:

    • Extend the controller to add custom filters (e.g., by severity or custom tags):
      namespace App\Http\Controllers;
      use Rap2hpoutre\LaravelLogViewer\LogViewerController as BaseController;
      
      class LogViewerController extends BaseController {
          public function index() {
              $logs = $this->getLogs()->filterBySeverity('error'); // Hypothetical method
              return view('vendor.laravel-log-viewer.log', ['logs' => $logs]);
          }
      }
      
    • Override the route to point to your extended controller.
  3. Integration with Log Rotation:

    • Ensure your logging.php config uses the daily channel for automatic log splitting (e.g., laravel-2023-10-01.log).
    • The package automatically detects and merges these files.
  4. CI/CD Logging:

    • Add the log viewer to your staging environment for post-deployment debugging.
    • Example route for CI logs:
      Route::get('ci-logs', [\Rap2hpoutre\LaravelLogViewer\LogViewerController::class, 'index'])
           ->middleware(['auth', 'env:staging']);
      

Gotchas and Tips

Common Pitfalls

  1. Config Cache Issues:

    • Symptom: InvalidArgumentException or blank page after installation.
    • Fix: Run php artisan config:clear and php artisan view:clear.
    • Prevention: Add this to your post-install scripts or package.json:
      "scripts": {
          "post-install": "php artisan config:clear"
      }
      
  2. Permission Errors:

    • Symptom: "File not readable" for storage/logs/laravel.log.
    • Fix: Ensure the web server user (e.g., www-data, apache) has read access:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/
      
  3. Log Rotation Confusion:

    • Gotcha: The package does not support compressed logs (e.g., .gz).
    • Workaround: Disable log compression in logging.php or pre-process logs:
      'channels' => [
          'single' => [
              'driver' => 'single',
              'path' => storage_path('logs/laravel.log'),
              'level' => 'debug',
              'compress' => false, // Disable compression
          ],
      ],
      
  4. Performance in Production:

    • Risk: Large log files (>50MB) cause slow page loads.
    • Mitigation:
      • Restrict access to trusted IPs or roles.
      • Truncate logs in production (e.g., via a cron job or logrotate).
      • Use the maxFile config to limit file size:
        'maxFile' => 10240, // 10MB
        

Debugging Tips

  1. Log Viewer Not Loading:

    • Check if the LogViewerController is registered in config/app.php.
    • Verify the route exists and is not overridden by another package.
  2. Custom View Not Applying:

    • Ensure the published view (resources/views/vendor/laravel-log-viewer/log.blade.php) is not cached.
    • Clear views: php artisan view:clear.
  3. Missing Logs:

    • Confirm logs are being written to the expected path (check config/logging.php).
    • Verify the log channel is not being overridden by environment-specific configs.

Extension Points

  1. Custom Log Parsing:

    • Override the LogViewerController to parse logs before rendering:
      public function index() {
          $logs = $this->getLogs()->map(function ($log) {
              $log['custom_field'] = $this->parseLog($log['message']);
              return $log;
          });
          return view('vendor.laravel-log-viewer.log', ['logs' => $logs]);
      }
      
  2. Add Export Functionality:

    • Extend the controller to add CSV/JSON export:
      public function export($format = 'json') {
          $logs = $this->getLogs();
          return response()->json($logs->toArray())->header('Content-Type', 'application/json');
      }
      
    • Add a route: Route::get('logs/export', [LogViewerController::class, 'export']);.
  3. Real-Time Logs:

    • Use Laravel Echo or a WebSocket package to stream logs in real-time (requires custom frontend integration).
  4. Multi-Environment Logs:

    • Dynamically set log paths based on the environment:
      'paths' => [
          env('LOG_PATH') ?: storage_path('logs/laravel.log'),
      ],
      

Security Reminders

  • Never expose /logs in production without strict authentication (e.g., IP whitelisting, 2FA).
  • Sanitize log outputs if displaying user-generated content to prevent XSS.
  • Avoid logging sensitive data (passwords, tokens) in the first place—use Laravel’s log() carefully.

Pro Tips

  • Keyboard Shortcuts: Use Ctrl+F for search and Esc to clear the search in most browsers.
  • Dark Mode: Add this to your custom log.blade.php for better readability:
    <style>
        body.log-viewer { background-color: #1a1a1a; color: #e0e0e0; }
        .log-entry { background-color: #2d2d2d; }
    </style>
    
  • Log Highlighting: Use the highlight config to customize syntax colors:
    'highlight' => [
        'error' => 'red',
        'warning' => 'orange',
        'info' => 'green',
    ],
    
  • Automate Log Cleanup: Combine with logrotate to keep logs manageable:
    # Example logrotate config (/etc/logrotate.d/laravel)
    /var/www/storage/logs/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 640 www-data:www-data
        sharedscripts
        postrotate
            invoke-rc.d apache2 reload > /dev/null 2>&1
        endscript
    }
    
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai