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

fishtail/laravel-log-viewer

Simple Laravel/Lumen log viewer for Laravel 5–8 (compatible with 4.2). Install via Composer, register the service provider, and add a route to LogViewerController@index. No published assets or vendor routes; works with rotated logs.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require rap2hpoutre/laravel-log-viewer
    
  2. Publish Config (Optional):

    php artisan vendor:publish --provider="Rap2hpoutre\LaravelLogViewer\LogViewerServiceProvider"
    

    (Default config works out-of-the-box, but publishing allows customization.)

  3. Route Integration: Add to routes/web.php:

    Route::get('/logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index');
    

    (Use middleware like auth if needed.)

  4. First Use Case: Visit /logs to view real-time Laravel logs with:

    • Filtering by log level (debug, info, warning, error, critical).
    • Search functionality.
    • Auto-refresh toggle (default: 30s).

Implementation Patterns

Core Workflows

  1. Log Filtering:

    • Level-Based: Use the dropdown to isolate errors (error, critical) or debug logs.
    • Keyword Search: Enter terms in the search bar to filter logs dynamically.
    • Date Range: Navigate via "Previous" and "Next" buttons to browse logs by day.
  2. Integration with Existing Systems:

    • Middleware: Restrict access to logs in production:
      Route::get('/logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index')->middleware('can:view-logs');
      
    • API Wrappers: Expose log data via a custom API endpoint by extending the controller:
      class ApiLogController extends \Rap2hpoutre\LaravelLogViewer\LogViewerController {
          public function showJson() {
              return response()->json($this->getLogs());
          }
      }
      
  3. Log Rotation Handling:

    • Automatically detects rotated logs (e.g., storage/logs/laravel-2023-10-01.log) and merges them into a unified view.
    • Configure log_viewer.log_files in config/log-viewer.php to specify custom paths or exclude files.
  4. Customizing Output:

    • Override the Blade view (resources/views/vendor/log-viewer/index.blade.php) to:
      • Add team-specific annotations.
      • Embed logs in a dashboard.
      • Modify the UI (e.g., dark mode, syntax highlighting for JSON logs).
  5. Programmatic Access:

    • Use the underlying service directly in tests or commands:
      $logs = app(\Rap2hpoutre\LaravelLogViewer\LogViewerService::class)->getLogs();
      

Advanced Patterns

  1. Log Annotations: Add context to logs by extending the LogEntry model:

    // In a service provider
    $this->app->extend('log-viewer.entry', function ($entry) {
        $entry->setCustomAttribute('user_id', auth()->id());
        return $entry;
    });
    
  2. Real-Time Updates: Combine with Laravel Echo/Pusher to push new logs to clients:

    // Frontend (Vue/React)
    Echo.channel('logs')
        .listen('LogUpdated', (e) => {
            // Update UI with new log entry
        });
    
  3. Log Export: Add a download button to export logs as JSON/CSV:

    // In a custom controller method
    public function exportLogs() {
        $logs = $this->getLogs();
        return response()->json($logs)->header('Content-Type', 'application/json');
    }
    

Gotchas and Tips

Pitfalls

  1. Log File Permissions:

    • Ensure storage/logs/ is writable by the web server:
      chmod -R 775 storage/logs/
      
    • Symptom: Blank page or "File not found" errors.
  2. Log Rotation Conflicts:

    • If logs are rotated manually (e.g., via logrotate), the viewer may miss recent entries.
    • Fix: Configure log_viewer.log_files to include all rotated files or adjust rotation settings to keep recent logs in a single file.
  3. Large Log Files:

    • Performance degrades with files >10MB. Use log_viewer.max_log_size to limit file size or pre-process logs with log_viewer.filter.
    • Tip: Archive old logs via a cron job to keep files manageable.
  4. Lumen Compatibility:

    • Lumen users must manually register the service provider in bootstrap/app.php:
      $app->register(\Rap2hpoutre\LaravelLogViewer\LogViewerServiceProvider::class);
      
  5. Monolog-Specific Issues:

    • If using custom Monolog handlers, ensure they output to storage/logs/laravel.log or update log_viewer.log_files.

Debugging

  1. Blank Screen:

    • Check storage/logs/laravel.log for PHP errors.
    • Verify the route is correctly pointing to LogViewerController.
  2. Logs Not Loading:

    • Temporarily disable log_viewer.filter in config to rule out custom filtering issues.
    • Test with a fresh log file to isolate corruption.
  3. Search Not Working:

    • Ensure log_viewer.searchable_fields includes all relevant log properties (default: ['message', 'context']).

Tips

  1. Configuration Shortcuts:

    • Disable auto-refresh for production:
      'auto_refresh' => env('APP_ENV') !== 'production',
      
    • Limit log visibility by level:
      'visible_levels' => ['error', 'critical'],
      
  2. Extending Functionality:

    • Add a "Clear Logs" button by extending the controller:
      public function clearLogs() {
          $files = glob(storage_path('logs/*.log'));
          foreach ($files as $file) {
              unlink($file);
          }
          return back()->with('status', 'Logs cleared!');
      }
      
  3. Security:

    • Rate-limit log access in production:
      Route::middleware(['throttle:10,1'])->group(function () {
          Route::get('/logs', 'LogViewerController@index');
      });
      
  4. Performance:

    • Cache log entries for non-real-time views:
      $logs = Cache::remember('log_viewer_entries', now()->addHours(1), function () {
          return $this->getLogs();
      });
      
  5. Local Development:

    • Use log_viewer.log_files to monitor specific log files:
      'log_files' => [
          storage_path('logs/laravel.log'),
          storage_path('logs/debug.log'),
      ],
      
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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
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