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

kolamitin/laravel-log-viewer

Lightweight Laravel (and Lumen) log viewer. Install via Composer, register the service provider, and add a route to LogViewerController to browse log files in the browser. No public 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
    

    (Note: The README mentions kolamitin/laravel-log-viewer, but the package is actually rap2hpoutre/laravel-log-viewer per the excerpt.)

  2. Publish Config (Optional):

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

    (Default config is minimal; publish only if customizing paths or filters.)

  3. Add Route:

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

    (Use middleware like auth or admin to restrict access.)

  4. First Use Case:

    • Access /logs in your browser to view formatted logs.
    • Filter by date, log level (e.g., error, info), or search content.

Implementation Patterns

Core Workflows

  1. Log Filtering:

    • Date Range: Use the UI dropdowns to select log files by date (default: last 7 days).
    • Log Levels: Filter by emergency, alert, critical, error, warning, notice, info, or debug.
    • Search: Enter keywords in the search box to narrow results (case-insensitive).
  2. Integration with Existing Logs:

    • Works with Laravel’s default single log file (storage/logs/laravel.log) or rotated logs (e.g., laravel-*.log).
    • No need to modify log drivers; the package reads files directly.
  3. Customization:

    • Override Views: Publish the package views (php artisan vendor:publish --tag=log-viewer-views) and modify:
      • resources/views/vendor/laravel-log-viewer/index.blade.php (main layout).
      • resources/views/vendor/laravel-log-viewer/partials/entry.blade.php (per-log entry styling).
    • Extend Functionality: Add custom filters or columns by extending the LogViewerController or creating a middleware to pre-process logs.
  4. Middleware Integration:

    • Restrict access to logs for non-developers:
      Route::get('/logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index')->middleware('can:view-logs');
      
  5. Programmatic Access:

    • Use the LogViewer facade to fetch logs programmatically:
      use Rap2hpoutre\LaravelLogViewer\Facades\LogViewer;
      
      $logs = LogViewer::getLogs()->filterByLevel('error')->get();
      

Advanced Patterns

  1. Log Rotation Handling:

    • The package automatically detects rotated logs (e.g., laravel-2023-10-01.log). No config needed for standard Laravel log rotation.
  2. Custom Log Paths:

    • Override the log path in config/log-viewer.php:
      'log_path' => storage_path('logs/custom'),
      
    • Useful for monorepos or multi-tenant apps with separate log directories.
  3. Localization:

    • Translate UI strings by publishing the language files and extending them:
      php artisan vendor:publish --tag=log-viewer-lang
      
  4. Performance:

    • For large log files (>10MB), add a limit to the controller:
      public function index()
      {
          return view('vendor.laravel-log-viewer.index', [
              'logs' => LogViewer::getLogs()->limit(1000)->get(),
          ]);
      }
      

Gotchas and Tips

Pitfalls

  1. Outdated Package:

    • Last release was in 2020. Test thoroughly in your Laravel version (5.x/6.x/Lumen).
    • Mitigation: Fork the repo or use a maintained alternative like spatie/laravel-log-viewer if critical.
  2. Log File Permissions:

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

    • If using custom log rotation (e.g., logrotate), ensure filenames match Laravel’s default pattern (laravel-YYYY-MM-DD.log).
    • Workaround: Extend the LogViewer service to support custom patterns.
  4. Memory Issues:

    • Loading years of logs can spike memory usage. Use the limit() method or paginate results:
      $logs = LogViewer::getLogs()->limit(500)->paginate(50);
      
  5. CSRF Token Errors:

    • The package may not include CSRF protection by default. Add it to the form in index.blade.php:
      @csrf
      

Debugging Tips

  1. Log File Path Issues:

    • Verify the log_path in config/log-viewer.php points to the correct directory.
    • Check for typos in the config key (e.g., log_path vs. path).
  2. Blank Page or 500 Error:

    • Enable Laravel’s debug mode (APP_DEBUG=true in .env) to see detailed errors.
    • Common causes:
      • Missing storage/logs directory (run php artisan storage:link).
      • PHP version incompatibilities (package targets PHP 5.6+; Laravel 5.8+ uses PHP 7.2+).
  3. Search Not Working:

    • The search is case-insensitive but may fail on special characters (e.g., ' or ").
    • Fix: Escape search terms or use str_contains instead of str_contains in the controller.

Extension Points

  1. Custom Log Formats:

    • Override the entry.blade.php partial to support structured logs (e.g., JSON):
      // In your custom entry.blade.php
      @php
          $logEntry = json_decode($entry['message'], true) ?? $entry['message'];
      @endphp
      <pre>{{ print_r($logEntry, true) }}</pre>
      
  2. Add Context Links:

    • Enhance log entries with clickable links (e.g., to user profiles):
      // In LogViewerController
      $logs = LogViewer::getLogs()->map(function ($log) {
          $log['message'] = str_replace(
              'user_id:123',
              '<a href="/users/123">user_id:123</a>',
              $log['message']
          );
          return $log;
      });
      
  3. API Endpoint:

    • Extend the controller to expose logs via an API:
      Route::get('/api/logs', function () {
          return response()->json(LogViewer::getLogs()->get());
      });
      
  4. Log Anonymization:

    • Strip sensitive data (e.g., passwords, tokens) before displaying logs:
      // In a middleware or service provider
      $logMessage = preg_replace('/"password":"[^"]+"/', '"password":"[FILTERED]"', $log['message']);
      
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.
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
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope