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

gopalindians/laravel-log-viewer

Lightweight log viewer for Laravel 6–13 and Lumen. Install via Composer and add a single route to LogViewerController—no public assets or vendor routes required. Works with rotated logs and supports view customization via publishing.

View on GitHub
Deep Wiki
Context7
## Getting Started
### Minimal Setup
1. **Installation**:
   ```bash
   composer require gopalindians/laravel-log-viewer

Verify the package is listed in composer.json under require.

  1. Publish Config (Optional):

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

    This creates a config/log-viewer.php file for customization (e.g., log paths, filters).

  2. Route Integration: Add the route in routes/web.php (or routes/api.php for API access):

    Route::get('/logs', [\Rap2hpoutre\LaravelLogViewer\Controller\LogViewerController::class, 'index']);
    

    For Lumen, register the route in bootstrap/app.php:

    $app->router->get('/logs', 'Rap2hpoutre\LaravelLogViewer\Controller\LogViewerController@index');
    
  3. First Use Case: Visit /logs in your browser. The package automatically detects Laravel’s default log paths (storage/logs/laravel.log and rotated files like laravel-2024-01-01.log). No additional configuration is needed for basic usage.


Implementation Patterns

Core Workflows

  1. Log Filtering:

    • Date Range: Use the date_from and date_to query parameters to filter logs by timestamp:
      /logs?date_from=2024-01-01&date_to=2024-01-31
      
    • Level Filtering: Filter by log levels (e.g., error, info) via level parameter:
      /logs?level=error
      
    • Search: Use the search parameter to find specific entries:
      /logs?search=Exception
      
  2. Custom Log Paths: Override default log paths in config/log-viewer.php:

    'log_paths' => [
        storage_path('logs/custom-app.log'),
        storage_path('logs/third-party.log'),
    ],
    
  3. Integration with Middleware: Restrict access to logs using Laravel middleware (e.g., auth or admin):

    Route::get('/logs', [LogViewerController::class, 'index'])->middleware('can:view-logs');
    
  4. API Access: Return logs as JSON for API consumption by modifying the controller or creating a dedicated endpoint:

    Route::get('/api/logs', [LogViewerController::class, 'apiIndex']);
    

    Extend the controller to support JSON responses:

    public function apiIndex(Request $request) {
        $logs = $this->getLogs($request);
        return response()->json($logs);
    }
    
  5. Log Rotation Handling: The package automatically handles rotated log files (e.g., laravel-*.log). No manual intervention is required.

Advanced Patterns

  • Log Anonymization: Use Laravel’s Log::shouldNotLog() or middleware to mask sensitive data before it reaches the logs. The viewer will display the sanitized output.

  • Log Archiving: Combine with Laravel’s Log::archive() or a cron job to move old logs to a separate directory (e.g., storage/logs/archive/). Update log_paths to include the archive path.

  • Real-Time Logs: For real-time log streaming, use Laravel Echo or a WebSocket solution (e.g., Pusher) to push new log entries to clients. The viewer itself is not real-time but can be extended to support it.


Gotchas and Tips

Common Pitfalls

  1. Permission Issues:

    • Ensure the web server user (e.g., www-data, nginx) has read access to storage/logs/.
    • Fix permissions with:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/  # Adjust user/group as needed
      
  2. Missing Log Files:

    • If logs aren’t appearing, verify Laravel’s logging configuration in config/logging.php. The package relies on the default single or daily channels.
    • Check if logs are being written to a non-standard path (e.g., /var/log/laravel.log). Update log_paths in the config.
  3. Rotated Logs Not Displayed:

    • The package supports rotated logs (e.g., laravel-2024-01-01.log), but if they’re missing, ensure Laravel’s log rotation is configured correctly in config/logging.php:
      'daily' => [
          'driver' => 'single',
          'path' => storage_path('logs/laravel.log'),
          'level' => env('LOG_LEVEL', 'debug'),
          'days' => 15, // Retain logs for 15 days
      ],
      
  4. Performance with Large Logs:

    • The viewer loads logs into memory. For logs >10MB, consider:
      • Filtering by date_from/date_to to reduce the dataset.
      • Using Laravel’s Log::getMonolog() to stream logs line-by-line (requires custom controller logic).
  5. Caching Issues:

    • The package does not cache log data by default. If you add caching (e.g., Redis), ensure it’s invalidated on new log entries to avoid stale data.

Debugging Tips

  1. Check Controller Output: Temporarily add debug output to the controller to verify log paths and filters:

    public function index(Request $request) {
        dd($this->getLogPaths()); // Inspect available log paths
        dd($request->all());      // Verify query parameters
    }
    
  2. Log Viewer Configuration: Dump the config to ensure settings are loaded:

    dd(config('log-viewer'));
    
  3. File Existence: Verify log files exist and are readable:

    ls -la storage/logs/
    cat storage/logs/laravel.log  # Check if logs are being written
    

Extension Points

  1. Customize the View: Publish the view file and modify it:

    php artisan vendor:publish --tag=log-viewer-views
    

    Edit resources/views/vendor/log-viewer/index.blade.php to add features like:

    • Syntax highlighting for log levels.
    • Collapsible log entries.
    • Export buttons (CSV/JSON).
  2. Add Log Actions: Extend the controller to add functionality like:

    • Clearing logs:
      public function clearLogs() {
          foreach ($this->getLogPaths() as $path) {
              if (file_exists($path)) unlink($path);
          }
          return back()->with('status', 'Logs cleared!');
      }
      
    • Download logs as a file:
      public function downloadLogs(Request $request) {
          $logs = $this->getLogs($request);
          return response()->streamDownload(function () use ($logs) {
              echo implode("\n", $logs);
          }, 'laravel-logs.txt');
      }
      
  3. Integrate with Laravel Debugbar: Use the Laravel Debugbar package to display recent logs in the debug toolbar:

    Debugbar::info('Logs', [
        'recent' => Log::getMonolog()->getLogs(),
    ]);
    
  4. Localization: Localize the viewer’s UI by publishing and translating the language file:

    php artisan vendor:publish --tag=log-viewer-lang
    

    Edit resources/lang/en/log-viewer.php.

  5. Security Hardening:

    • Restrict access to the log viewer route using Laravel gates/policies:
      Gate::define('view-logs', function ($user) {
          return $user->isAdmin();
      });
      
    • Rate-limit access to prevent log scraping:
      Route::middleware(['throttle:60,1'])->group(function () {
          Route::get('/logs', [LogViewerController::class, 'index']);
      });
      

---
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