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 Vv Logs Viewer Laravel Package

ebethus/laravel-vv-logs-viewer

Simple in-app Laravel/Lumen log viewer. Install via Composer, register the service provider, and add a route to LogViewerController@index to browse log files (with or without log rotation). No public assets or vendor routes needed.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require rap2hpoutre/laravel-log-viewer
    

    Update config/app.php to include the service provider:

    'providers' => [
        // ...
        Rap2hpoutre\LaravelLogViewer\LogViewerServiceProvider::class,
    ],
    
  2. Publish Config (Optional):

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

    This creates a config/log-viewer.php file for customization.

  3. Route the Controller: Add this to your routes/web.php:

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

    (Note: For Laravel 8+, use use statement or fully qualified class name.)

  4. First Use Case: Visit /logs in your browser. The package will display a paginated list of log files (e.g., laravel.log, rotated logs like laravel-2023-10-01.log), with the ability to view their contents line-by-line.


Implementation Patterns

Core Workflows

  1. Log File Discovery:

    • The package auto-detects log files in storage/logs/ (default) or a custom path (configurable via log-viewer.php).
    • Supports rotated logs (e.g., laravel.log, laravel-*.log) out of the box.
  2. Viewing Logs:

    • List View: Displays all log files with metadata (size, last modified).
    • Detail View: Clicking a log file shows its contents in a paginated table (configurable entries per page).
    • Search: Filter logs by keyword (case-insensitive) across all files or a specific file.
  3. Integration with Laravel Debugging:

    • Use alongside dd() or dump() for quick log inspection during development.
    • Replace tail -f storage/logs/laravel.log with the web interface for real-time monitoring.
  4. Customization:

    • Templates: Override the Blade views in resources/views/vendor/log-viewer/ to match your app’s design.
    • Middleware: Protect the route with auth:
      Route::get('/logs', function () {
          return app('log-viewer')->index();
      })->middleware('auth');
      
    • Log Path: Change the log directory in config/log-viewer.php:
      'log_path' => storage_path('custom-logs'),
      
  5. Programmatic Access:

    • Access the log viewer directly in code (e.g., for CLI tools or tests):
      $logs = app('log-viewer')->getLogs();
      $logContent = app('log-viewer')->getLogContent('laravel.log');
      

Advanced Patterns

  1. Log Filtering by Level:

    • Extend the package to filter logs by Monolog levels (e.g., error, info). Modify the LogViewerController to pass a $level parameter to the getLogs() method or override the view logic.
  2. Export Logs:

    • Add a route to export logs as a ZIP file:
      Route::get('/logs/export', function () {
          return app('log-viewer')->exportLogs();
      });
      
    • Implement the exportLogs() method in a service class.
  3. Real-Time Updates:

    • Use Laravel Echo/Pusher to push new log entries to the frontend in real-time. Requires tailing the log file and emitting events.
  4. Log Anonymization:

    • Pre-process log content to redact sensitive data (e.g., passwords, tokens) before displaying it. Hook into the getLogContent() method or create a middleware.

Gotchas and Tips

Pitfalls

  1. Permission Issues:

    • Ensure the web server user (e.g., www-data, nginx) has read access to storage/logs/.
    • Fix with:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/
      
  2. Rotated Logs Not Showing:

    • The package relies on Laravel’s default log rotation naming convention (laravel-{date}.log). If using a custom rotator, ensure the log-viewer.php config includes the correct glob pattern:
      'log_pattern' => 'laravel-*.log',
      
  3. Large Log Files:

    • Paginating large log files (e.g., 100MB+) may cause memory issues. Adjust the per_page setting in config/log-viewer.php or optimize the log rotation strategy.
  4. Caching Headaches:

    • The package caches log file listings for performance. Clear the cache if logs aren’t updating:
      php artisan cache:clear
      
    • Disable caching in config/log-viewer.php for development:
      'cache_enabled' => false,
      
  5. Lumen Compatibility:

    • In Lumen, manually bind the service provider in bootstrap/app.php:
      $app->register(Rap2hpoutre\LaravelLogViewer\LogViewerServiceProvider::class);
      
  6. CSRF Protection:

    • The package doesn’t include CSRF protection by default. Add it to the route if needed:
      Route::get('/logs', 'LogViewerController@index')->middleware('web');
      

Debugging Tips

  1. Log Viewer Not Loading:

    • Check if the service provider is registered and the route is correctly defined.
    • Verify the storage/logs/ directory exists and contains log files.
  2. Blank Page:

    • Ensure the resources/views/vendor/log-viewer/ directory exists (publish the views if missing).
    • Check for PHP errors in storage/logs/laravel.log.
  3. Search Not Working:

    • The search is case-insensitive but may fail with special characters. Escape search terms or use regex:
      // Example: Modify the search logic in LogViewerController
      preg_match('/' . preg_quote($searchTerm, '/') . '/i', $line)
      
  4. Performance Bottlenecks:

    • For slow loading, profile the getLogContent() method. Large files may benefit from streaming or chunked reading.

Extension Points

  1. Custom Log Sources:

    • Extend the LogViewerService to support non-Laravel log files (e.g., Nginx, MySQL). Override the getLogs() method to include additional paths or parsers.
  2. Log Formatting:

    • Modify the Blade template to highlight log levels or add collapsible sections for nested data (e.g., JSON dumps).
  3. API Endpoint:

    • Convert the log viewer into a JSON API for use in SPAs or mobile apps. Example:
      Route::get('/api/logs', function () {
          return response()->json(app('log-viewer')->getLogs());
      });
      
  4. Log Archiving:

    • Add a feature to archive old logs (e.g., move to storage/logs/archive/) via a scheduled command:
      // Example: app/Console/Commands/ArchiveLogs.php
      use Rap2hpoutre\LaravelLogViewer\LogViewerService;
      
      class ArchiveLogs extends Command {
          protected $logViewer;
      
          public function __construct(LogViewerService $logViewer) {
              $this->logViewer = $logViewer;
          }
      
          public function handle() {
              $this->logViewer->archiveOldLogs();
          }
      }
      
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.
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
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium