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

elkadrey/laravel-log-viewer

Lightweight Laravel/Lumen log viewer. Install via Composer, register the service provider, and add a route to LogViewerController@index to browse your app logs in the browser. No public assets or vendor routes; supports rotated logs.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require elkadrey/laravel-log-viewer
    

    (Note: The README mentions rap2hpoutre/laravel-log-viewer, but the package name in the prompt is elkadrey/laravel-log-viewer. Verify the correct package name in your project.)

  2. Publish Configuration (optional):

    php artisan vendor:publish --provider="Elkadrey\LogViewer\LogViewerServiceProvider"
    

    (This generates a config/log-viewer.php file for customization.)

  3. Add Route: In routes/web.php:

    Route::get('/logs', 'Elkadrey\LogViewer\LogViewerController@index');
    

    (For Lumen, use Route::get('/logs', 'Elkadrey\LogViewer\LogViewerController@index'); in routes/web.php.)

  4. First Use Case: Visit /logs in your browser to view Laravel’s log files (e.g., storage/logs/laravel.log) in a structured UI. Supports filtering by date, level (e.g., error, info), and keyword search.


Implementation Patterns

Core Workflows

  1. Log Filtering:

    • Date Range: Use the UI dropdowns to select a time range (e.g., "Last 24 hours").
    • Log Levels: Filter by debug, info, warning, error, or critical via checkboxes.
    • Keyword Search: Enter terms in the search bar to narrow logs (e.g., auth, database).
    • Example: Debug a failed payment by filtering for error logs from the last hour containing stripe.
  2. Integration with Existing Logs:

    • Works seamlessly with Laravel’s default logging (Monolog). No changes to your config/logging.php are needed.
    • Supports rotated logs (e.g., laravel-2025-04-06.log) out of the box.
  3. Custom Log Files:

    • Extend the viewer to include custom log files (e.g., storage/logs/custom.log) by modifying the LogViewerServiceProvider:
      $this->app['log-viewer']->addLogFile('custom.log');
      
    • Use Case: Monitor third-party library logs (e.g., queue-worker.log).
  4. API Access (Advanced):

    • Expose logs via an API endpoint by creating a custom controller extending LogViewerController:
      use Elkadrey\LogViewer\LogViewerController;
      
      class ApiLogViewerController extends LogViewerController {
          public function index() {
              $logs = parent::getLogs(); // Reuse existing logic
              return response()->json($logs);
          }
      }
      
    • Use Case: Build a frontend dashboard or integrate with monitoring tools (e.g., Grafana).
  5. Log Annotations:

    • Add context to logs by extending the LogEntry model (if the package uses one) or pre-processing logs with metadata:
      // In a middleware or service provider
      Log::withContext(['user_id' => auth()->id(), 'request_id' => request()->header('X-Request-ID')]);
      
    • Use Case: Correlate logs to user sessions or API requests.

Gotchas and Tips

Pitfalls

  1. Log Rotation Conflicts:

    • If logs are rotated frequently (e.g., daily), the viewer may not show the latest logs immediately. Ensure log-max-files in config/logging.php is set appropriately (e.g., 30).
    • Fix: Manually trigger log rotation with php artisan log:rotate or adjust the rotation schedule.
  2. Permission Issues:

    • The viewer reads storage/logs/, which may require permissions:
      chmod -R 775 storage/logs
      
    • Tip: Use Laravel’s storage:link to ensure symlinks are accessible.
  3. Large Log Files:

    • Loading multi-GB log files may cause timeouts or memory issues. The package likely buffers logs in chunks, but test with:
      // In config/log-viewer.php
      'chunk_size' => 1000, // Process 1000 lines at a time
      
    • Tip: Use tail -f in production to stream logs in real-time (requires custom integration).
  4. Lumen Compatibility:

    • Lumen’s minimal setup may require manual route binding. Ensure the LogViewerServiceProvider is registered in bootstrap/app.php:
      $app->register(\Elkadrey\LogViewer\LogViewerServiceProvider::class);
      
  5. Caching Logs:

    • The viewer may cache log entries for performance. Clear the cache if logs aren’t updating:
      php artisan cache:clear
      
    • Tip: Disable caching in config/log-viewer.php for development:
      'cache' => env('APP_ENV') !== 'local',
      

Debugging Tips

  1. Log Entry Parsing:

    • If logs appear malformed, check the LogEntry parser (if the package uses one). Override the parser in a service provider:
      $this->app->bind('log-viewer.parser', function() {
          return new CustomLogParser();
      });
      
    • Example: Handle JSON-formatted logs by extending the parser.
  2. Missing Logs:

    • Verify logs are written to storage/logs/laravel.log. Test with:
      Log::error('Test log entry');
      
    • Check Laravel’s APP_LOG environment variable to confirm the log channel.
  3. Performance Bottlenecks:

    • Use Laravel Debugbar (barryvdh/laravel-debugbar) to profile the /logs route:
      composer require barryvdh/laravel-debugbar
      
    • Tip: Add an index to log files if searching is slow (e.g., use sqlite for indexed logs).

Extension Points

  1. Custom Log Formats:

    • Extend the viewer to support non-Monolog formats (e.g., syslog) by implementing a LogParser interface:
      class SyslogParser implements LogParser {
          public function parse($logEntry) { ... }
      }
      
    • Register it in the service provider:
      $this->app->bind('log-viewer.parser', SyslogParser::class);
      
  2. UI Customization:

    • Override the Blade views in resources/views/vendor/log-viewer/ to modify the UI (e.g., add a dark mode toggle).
    • Example: Extend index.blade.php to include a "Copy to Clipboard" button for log entries.
  3. Alerting Integration:

    • Hook into the log viewer to trigger alerts (e.g., Slack notifications for error logs). Use a service provider:
      $this->app->afterResolving('log-viewer', function($viewer) {
          $viewer->setAlertCallback(function($log) {
              if (strpos($log->message, 'failed') !== false) {
                  // Send alert
              }
          });
      });
      
  4. Log Archiving:

    • Automate log archiving by extending the LogViewer class to move old logs to storage/logs/archive/:
      $viewer->archiveLogs('laravel.log', 30); // Archive logs older than 30 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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky