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

Logs Viewer Laravel Package

vietstars/logs-viewer

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Run composer require vietstars/logs-viewer in your Laravel 10 project.

  2. Register the Service Provider Add this to bootstrap/app.php:

    $app->register(\Vietstars\LogsViewer\LogsViewerServiceProvider::class);
    
  3. Define Routes Add this to routes/web.php (or your preferred route file):

    Route::get('logs', [\Vietstars\LogsViewer\Controllers\LogsViewerController::class, 'index'])->name('logs.viewer');
    

    (Note: The README mentions app/Http/routes.php, but Laravel 10+ uses routes/web.php.)

  4. Access Logs Visit /logs in your browser to view Laravel’s log files.


First Use Case: Debugging in Production

  • Quick Inspection: Use the /logs endpoint to inspect logs without tailing files manually.
  • Filtering: Leverage the default search bar to filter logs by keywords (e.g., error, auth, 500).
  • No CLI Needed: Debug issues directly from the browser, reducing context-switching.

Implementation Patterns

Workflows

  1. Development Workflow

    • Local Debugging: Use the viewer to monitor logs in real-time during development.
    • Collaboration: Share the /logs endpoint with teammates to debug issues collaboratively.
  2. Production Workflow

    • Incident Response: Quickly filter logs for errors during deployments or outages.
    • Audit Trails: Search for specific user actions or API calls (e.g., user_id:123).
  3. Integration with Monitoring

    • Alert Correlation: Use the viewer to cross-reference logs with monitoring tools (e.g., Sentry, Datadog).
    • Log Sampling: Export filtered logs via the UI for deeper analysis in tools like ELK or Splunk.

Integration Tips

  1. Customize the View Publish and modify the Blade template:

    php artisan vendor:publish --provider="Vietstars\LogsViewer\LogsViewerServiceProvider" --tag=views
    
    • Add custom columns (e.g., severity levels, stack trace highlights).
    • Include contextual links (e.g., redirect to a specific resource after searching for its ID).
  2. Extend Functionality

    • Add Filters: Override the controller to add custom filters (e.g., by HTTP method or IP).
      // app/Http/Controllers/LogsViewerController.php
      public function index(Request $request) {
          $logs = Log::query()->when($request->filled('method'), function($query) use ($request) {
              return $query->where('context.method', $request->method);
          })->get();
          return view('logs-viewer::log', compact('logs'));
      }
      
    • Log Anonymization: Modify the view to redact sensitive data (e.g., passwords, tokens) before display.
  3. Authentication Protect the route with middleware (e.g., auth, admin):

    Route::get('/logs', [\Vietstars\LogsViewer\Controllers\LogsViewerController::class, 'index'])
         ->middleware('can:view-logs')
         ->name('logs.viewer');
    
  4. Configuration Publish the config file for adjustments:

    php artisan vendor:publish --provider="Vietstars\LogsViewer\LogsViewerServiceProvider"
    
    • Set log_file to customize the path (e.g., storage/logs/custom.log).
    • Adjust per_page for pagination limits or max_lines to control log volume.

Gotchas and Tips

Pitfalls

  1. Performance Overhead

    • Large Logs: Loading millions of lines may time out or freeze the browser. Use max_lines in config to limit results.
    • Database Logs: If using Log::channel('database'), ensure your database can handle the query load.
  2. Route Conflicts

    • The default /logs route may conflict with other packages. Rename it in routes/web.php:
      Route::get('/admin/logs', [\Vietstars\LogsViewer\Controllers\LogsViewerController::class, 'index']);
      
  3. Log File Permissions

    • Ensure the web server user (e.g., www-data, nginx) has read access to the log file:
      chmod 644 storage/logs/laravel.log
      
  4. Real-Time Updates

    • The viewer does not support live tailing. For real-time logs, consider:
      • Polling the endpoint with JavaScript (e.g., setInterval).
      • Using Laravel Echo + WebSockets for push notifications.

Debugging Tips

  1. Log Not Showing?

    • Verify the log file path in config/logging.php matches the log_file in config/logviewer.php.
    • Check if the log file exists and is writable:
      ls -la storage/logs/
      touch storage/logs/laravel.log
      
  2. Blank Page?

    • Clear cached views:
      php artisan view:clear
      
    • Check for Blade syntax errors in the published log.blade.php.
  3. Search Not Working

    • Ensure logs contain the search term (case-sensitive by default). Override the search logic in the controller:
      $logs = Log::query()->where('message', 'like', "%{$request->search}%")->get();
      

Extension Points

  1. Custom Log Channels Extend the controller to support multiple log channels (e.g., single, daily, slack):

    public function index(Request $request) {
        $channel = $request->input('channel', 'single');
        $logs = Log::channel($channel)->query()->get();
        return view('logs-viewer::log', compact('logs'));
    }
    
  2. Log Highlighting Use JavaScript to syntax-highlight log entries (e.g., JSON, stack traces):

    // resources/js/logs-viewer.js
    document.querySelectorAll('.log-entry').forEach(entry => {
        entry.innerHTML = entry.textContent.replace(/({.*?})/g, '<span class="json">$1</span>');
    });
    
  3. Export Functionality Add a download button to export filtered logs as a JSON/CSV file:

    // In LogsViewerController
    public function export(Request $request) {
        $logs = Log::query()->where('message', 'like', "%{$request->search}%")->get();
        return response()->json($logs->toArray());
    }
    
  4. Log Retention Integrate with Laravel’s log rotation (config/logging.php) to auto-clean old logs:

    'single' => [
        'driver' => 'single',
        'path' => storage_path('logs/laravel.log'),
        'level' => env('LOG_LEVEL', 'debug'),
        'days' => 7, // Retain logs for 7 days
    ],
    
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.
craftcms/url-validator
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