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 View Laravel Package

zha/laravel-log-view

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require zha/laravel-log-view
    php artisan vendor:publish --provider="Zha\LaravelLogView\LogViewerServiceProvider" --tag="log-viewer-public"
    
    • Publishes the required CSS assets to public/vendor/laravel-log-view.
  2. Access Logs:

    • Visit /log-viewer in your browser to view Laravel logs in a web interface.
    • Defaults to displaying logs from storage/logs/laravel.log.
  3. First Use Case:

    • Debug production issues by filtering logs via the UI (e.g., search by timestamp, error level, or keyword).
    • Quickly identify recent errors without tailing log files manually.

Implementation Patterns

Core Workflows

  1. Log Filtering:

    • Use the UI dropdowns to filter logs by:
      • Level (e.g., error, warning, info).
      • Date range (predefined or custom).
    • Example: Isolate error logs from the last 24 hours to triage a critical issue.
  2. Integration with Existing Logs:

    • The package reads Laravel’s default log files (laravel.log, laravel-*.log). Ensure your config/logging.php channels are configured to write to these files.
    • For custom log channels, extend the package by overriding the LogViewerServiceProvider to include additional paths.
  3. Programmatic Access:

    • Access log data programmatically via the LogViewer facade:
      use Zha\LaravelLogView\Facades\LogViewer;
      $logs = LogViewer::getLogs(['level' => 'error', 'days' => 1]);
      
    • Useful for building custom admin dashboards or automated alerts.
  4. Middleware/Route Protection:

    • Secure the /log-viewer route by adding middleware (e.g., auth or role:admin) in routes/web.php:
      Route::middleware(['auth'])->get('/log-viewer', [LogViewerController::class, 'index']);
      
  5. Log Rotation Handling:

    • If using log rotation (e.g., laravel-log-rotate), ensure the package’s file scanner (Zha\LaravelLogView\LogViewer) is updated to handle rotated files. Override the getLogFiles() method in a service provider if needed.

Gotchas and Tips

Pitfalls

  1. File Permissions:

    • Ensure the web server user (e.g., www-data, nginx) has read access to storage/logs/. Run:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/  # Adjust user/group as needed
      
    • Symptoms: Blank page or "File not found" errors.
  2. Log File Encoding:

    • The package assumes UTF-8 encoded logs. If logs are encoded differently (e.g., GBK), override the readLogFile() method in the LogViewer class to handle encoding.
  3. Large Log Files:

    • Performance may degrade with log files >100MB. Optimize by:
      • Increasing PHP’s memory_limit temporarily during log viewing.
      • Using the limit parameter in the UI or API to paginate results:
        LogViewer::getLogs(['limit' => 1000]);
        
  4. Custom Log Paths:

    • The package defaults to storage/logs/laravel.log. To include additional paths (e.g., storage/logs/custom.log), extend the LogViewer class:
      namespace App\Services;
      use Zha\LaravelLogView\LogViewer as BaseLogViewer;
      
      class CustomLogViewer extends BaseLogViewer {
          protected function getLogFiles() {
              return array_merge(parent::getLogFiles(), ['storage/logs/custom.log']);
          }
      }
      
      Bind the custom class in a service provider:
      $this->app->bind('logviewer', function () {
          return new CustomLogViewer();
      });
      
  5. Caching Issues:

    • Logs are read directly from files (no caching). For high-traffic environments, consider caching log entries in Redis or the database, but ensure cache invalidation on new log writes.

Debugging Tips

  1. Check Published Assets:

    • If the UI appears broken, verify the CSS was published:
      ls public/vendor/laravel-log-view/
      
    • Re-publish if missing:
      php artisan vendor:publish --tag="log-viewer-public" --force
      
  2. LogViewer Facade Not Found:

    • Ensure the service provider is registered in config/app.php under providers. If using Laravel <8.x, manually add:
      Zha\LaravelLogView\LogViewerServiceProvider::class,
      
  3. Route Conflicts:

    • The package registers /log-viewer automatically. If this conflicts with existing routes, override the route in your RouteServiceProvider:
      Route::get('/custom-log-viewer', [LogViewerController::class, 'index'])->name('log-viewer');
      

Extension Points

  1. Custom Log Parsers:

    • Extend the LogViewer class to parse structured logs (e.g., JSON). Override the parseLogEntry() method:
      protected function parseLogEntry(string $entry): array {
          $data = json_decode($entry, true);
          return [
              'level' => $data['level'] ?? 'info',
              'message' => $data['message'] ?? $entry,
              'context' => $data['context'] ?? [],
          ];
      }
      
  2. Add Log Actions:

    • Integrate with the UI to add buttons for common actions (e.g., "Clear logs" or "Download as JSON"). Extend the LogViewerController:
      public function downloadLogs(Request $request) {
          $logs = LogViewer::getLogs($request->all());
          return response()->json($logs)->header('Content-Type', 'application/json');
      }
      
      Add a route and JavaScript to trigger this action.
  3. Multi-Tenant Logs:

    • For multi-tenant apps, filter logs by tenant ID. Override the getLogFiles() method to include tenant-specific paths:
      protected function getLogFiles() {
          $tenantId = auth()->user()->tenant_id;
          return ["storage/logs/tenant_{$tenantId}/laravel.log"];
      }
      
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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
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