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

everysecond/laravel-log-viewer

Lightweight Laravel/Lumen log viewer. Install via Composer and add a route to LogViewerController to browse application logs in the browser. No public assets or vendor routes required; supports rotated logs and works across multiple Laravel versions.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require rap2hpoutre/laravel-log-viewer
    

    (Note: The README incorrectly lists everysecond/laravel-log-viewer; the correct package is rap2hpoutre/laravel-log-viewer.)

  2. Publish Config (Optional):

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

    (Default config is minimal; customization is rare but useful for log paths.)

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

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

    (No middleware needed by default; secure with auth or can if required.)

  4. First Use Case: Visit /logs to see a real-time log viewer with:

    • Auto-refresh (configurable interval).
    • Filtering by log level (e.g., error, info).
    • Line numbers and timestamps.

Implementation Patterns

Core Workflows

  1. Debugging in Development:

    • Quick Checks: Use /logs to verify if a bug’s logs exist before diving into storage/logs.
    • Real-Time Monitoring: Watch logs live during feature testing (e.g., API endpoints, cron jobs).
    • Filtering: Append ?level=error to the URL to focus on critical logs.
  2. Production Support:

    • Limited Access: Restrict the route to admins only:
      Route::get('/logs', [LogViewerController::class, 'index'])->middleware('can:view-logs');
      
    • Log Rotation Handling: Works seamlessly with Laravel’s default log rotation (no manual config needed).
  3. Integration with Existing Tools:

    • Log Analysis: Export logs via the built-in "Download" button for further analysis (e.g., in Sentry or Datadog).
    • Custom Log Channels: If using non-default log channels (e.g., single, daily), ensure the log config path is correct in config/log-viewer.php.
  4. Testing:

    • Mock Logs: Simulate logs in tests by writing to storage/logs/laravel.log and verifying the viewer displays them.
    • Assertions: Use Laravel’s Log::shouldReceive() to test log filtering logic.

Advanced Patterns

  • Custom Log Paths: Override the log path in config:
    'log_path' => storage_path('logs/custom/laravel.log'),
    
  • Disable Auto-Refresh: Set 'auto_refresh' => false in config to reduce server load.
  • Theming: Extend the Blade template (resources/views/vendor/log-viewer/index.blade.php) to match your app’s CSS.

Gotchas and Tips

Pitfalls

  1. Incorrect Package Name: The README lists everysecond/laravel-log-viewer, but the correct package is rap2hpoutre/laravel-log-viewer. Double-check during installation.

  2. Log Rotation Conflicts:

    • If logs are rotated (e.g., laravel.log.1), the viewer may not show them by default. Use the "Browse" feature to manually select older files.
    • Fix: Ensure log-rotate isn’t deleting logs before they’re viewed.
  3. Performance on Large Logs:

    • Auto-refresh on logs >10MB may lag. Disable it or increase PHP’s memory_limit temporarily.
    • Tip: Use ?lines=100 to limit displayed lines.
  4. Permission Issues:

    • Ensure the web server user (e.g., www-data) has read access to storage/logs/.
    • Fix:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/
      
  5. Lumen Compatibility:

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

Debugging Tips

  • Logs Not Showing?: Verify the log path in config/log-viewer.php matches your config/logging.php path setting.
    // config/logging.php
    'path' => storage_path('logs/laravel.log'),
    
  • Blank Page: Check for PHP errors in storage/logs/laravel.log (ironic but common). Enable Laravel’s debug mode:
    APP_DEBUG=true
    
  • Filtering Not Working: Ensure log levels match Laravel’s defaults (debug, info, notice, warning, error, critical). Custom levels (e.g., spam) won’t filter.

Extension Points

  1. Custom Log Levels: Extend the LogLevel class to support custom levels:

    namespace App\Extensions;
    use Rap2hpoutre\LaravelLogViewer\LogLevel;
    
    class CustomLogLevel extends LogLevel {
        public static function spam(): string { return 'spam'; }
    }
    

    Then update the controller to use your class.

  2. Add Context to Logs: Override the LogViewerController to inject additional context (e.g., user ID, request data) into log lines:

    public function index() {
        Log::info('Custom context', ['user_id' => auth()->id()]);
        return parent::index();
    }
    
  3. API Endpoint: Convert the viewer into an API by extending the controller:

    public function apiLogs(Request $request) {
        return response()->json($this->getLogs($request));
    }
    

    (Useful for integrating with frontend frameworks like Vue/React.)

  4. Log Anonymization: Filter sensitive data (e.g., passwords) before displaying logs. Hook into the getLogs method:

    protected function filterLogs(array $logs) {
        return array_map(function ($log) {
            return str_replace('password=".*?"', 'password="[FILTERED]"', $log);
        }, $logs);
    }
    
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.
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
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle