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

nmfmcosta/laravel-log-viewer

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:
    composer require nmfmcosta/laravel-log-viewer
    
  2. Register Service Provider (Laravel): Add to config/app.php under providers:
    LB\LaravelLogViewer\LaravelLogViewerServiceProvider::class,
    
  3. Add Route (Laravel): In routes/web.php:
    Route::get('logs', '\LB\LaravelLogViewer\LogViewerController@index');
    
  4. Access Logs: Visit /logs in your browser to view Laravel’s default log file (storage/logs/laravel.log).

First Use Case

  • Debugging Live Issues: Quickly inspect real-time logs without tailing files manually.
  • Production Troubleshooting: Filter logs by severity (e.g., error, critical) to isolate issues in staging/production.

Implementation Patterns

Core Workflows

  1. Log Filtering:

    • Use query parameters to filter logs by:
      • level: debug, info, warning, error, critical.
      • date: ?date=2024-01-01 (format: YYYY-MM-DD).
      • search: Full-text search (e.g., ?search=authentication).
    • Example:
      /logs?level=error&date=2024-01-01
      
  2. Integration with Laravel Debugbar:

    • Add a link to the log viewer in your Debugbar panel for seamless navigation:
      Debugbar::addMenu('Logs', [
          'link' => route('logs'),
          'text' => 'View Logs',
      ]);
      
  3. Custom Log Files:

    • Extend the package to support additional log files (e.g., storage/logs/custom.log):
      • Publish the config (php artisan vendor:publish --tag=config) and update logviewer.php:
        'files' => [
            storage_path('logs/laravel.log'),
            storage_path('logs/custom.log'),
        ],
        
  4. API Access (Lumen/Laravel):

    • Expose logs via an API endpoint for headless clients:
      Route::get('api/logs', '\LB\LaravelLogViewer\LogViewerController@apiIndex');
      
    • Customize the apiIndex method in the controller to return JSON:
      public function apiIndex() {
          return response()->json($this->getLogs());
      }
      

Advanced Patterns

  • Log Anonymization:

    • Override the LogViewerController to sanitize sensitive data (e.g., passwords, tokens) before rendering:
      protected function formatLogEntry($entry) {
          $entry['message'] = str_replace('password=.*?&', 'password=***&', $entry['message']);
          return $entry;
      }
      
  • Log Retention:

    • Configure log rotation in logviewer.php to limit displayed logs (e.g., last 7 days):
      'max_days' => 7,
      
  • Real-Time Updates:

    • Use Laravel Echo/Pusher to push new log entries to connected clients (requires frontend JS integration).

Gotchas and Tips

Pitfalls

  1. Permission Issues:

    • Ensure the web server user (e.g., www-data, nginx) has read access to storage/logs/:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/
      
  2. Large Log Files:

    • Performance degrades with log files >100MB. Use max_days in config to limit scope:
      'max_days' => 30,
      
  3. Route Conflicts:

    • Avoid naming your log route /logs if it clashes with existing routes. Use a subpath like /admin/logs.
  4. Lumen Namespace:

    • In Lumen, ensure the namespace in bootstrap/app.php matches the route definition exactly to avoid ClassNotFound errors.

Debugging

  • Blank Page?:

    • Check if the logviewer.php config file exists in /config/. Publish it if missing:
      php artisan vendor:publish --tag=config
      
    • Verify the files array includes the correct log path.
  • Search Not Working:

    • The search is case-sensitive. Use ?search=Error (not error) for consistency with log levels.
  • Logs Not Updating:

    • Clear cached views if changes to log.blade.php aren’t reflected:
      php artisan view:clear
      

Tips

  1. Custom Styling:

    • Override the default Blade template (resources/views/vendor/laravel-log-viewer/log.blade.php) to match your app’s design:
      <style>
          .log-entry.error { background: #ffebee; }
          .log-entry.debug { font-style: italic; }
      </style>
      
  2. Environment-Specific Routes:

    • Restrict log access to local environments in routes/web.php:
      if (app()->environment('local')) {
          Route::get('logs', '\LB\LaravelLogViewer\LogViewerController@index');
      }
      
  3. Log Level Aliases:

    • Use shorthand for log levels in URLs:
      /logs?level=e  # Equivalent to ?level=error
      
    • Add this to your logviewer.php:
      'level_aliases' => [
          'e' => 'error',
          'c' => 'critical',
      ],
      
  4. Log Context:

    • Enhance log readability by adding context (e.g., user IDs, request IDs) to log entries:
      Log::debug('User action', ['user_id' => auth()->id(), 'request_id' => request()->header('X-Request-ID')]);
      
  5. Backup Logs:

    • Exclude sensitive logs from the viewer by filtering in logviewer.php:
      'exclude_patterns' => [
          'password',
          'api_token',
          'secret_',
      ],
      
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.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony
spatie/flare-daemon-runtime