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

Log Viewer Laravel Laravel Package

logviewerlaravel/log-viewer-laravel

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require logviewerlaravel/log-viewer-laravel
    

    Add the following to routes/web.php:

    Route::get('logs', [\LogViewerLaravel\LogViewerController::class, 'index'])->name('log.viewer');
    Route::get('logs/logs_view', [\LogViewerLaravel\LogViewerController::class, 'view']);
    
  2. First Use Case:

    • Access /logs in your browser to view Laravel’s default log file (storage/logs/laravel.log).
    • No additional setup is required for basic functionality.
  3. Where to Look First:

    • Route Definitions: Verify the routes are correctly mapped in routes/web.php.
    • Published Views: Check /resources/views/vendor/laravel-log-viewer/ if customization is needed.
    • Configuration: Review /config/logviewer.php (publish it first if needed) for log file paths, filters, or display settings.

Implementation Patterns

Usage Patterns

  1. Basic Log Viewing:

    • Use the default /logs route for a quick, unfiltered view of logs.
    • Ideal for debugging during development or troubleshooting production issues in staging environments.
  2. Customized Log Display:

    • Publish the default view:
      php artisan vendor:publish --provider="ViewerLogic\LogViewerLaravelProvider" --tag=views
      
    • Modify /resources/views/vendor/laravel-log-viewer/log.blade.php to:
      • Add syntax highlighting for specific log levels (e.g., error, info).
      • Include timestamps or user context (e.g., authenticated user’s IP/ID).
      • Integrate with Laravel’s Blade components (e.g., dropdown filters, pagination).
  3. Configuration Overrides:

    • Publish the config file:
      php artisan vendor:publish --provider="ViewerLogic\LogViewerLaravelProvider"
      
    • Customize /config/logviewer.php to:
      • Change the default log file path (e.g., for multi-tenant apps):
        'log_file' => storage_path('logs/custom-app.log'),
        
      • Set log level filters (e.g., hide debug logs in production):
        'levels' => ['error', 'warning', 'info'],
        
      • Adjust the number of logs displayed per page:
        'per_page' => 100,
        
  4. Integration with Laravel Features:

    • Middleware: Restrict access to the log viewer in production:
      Route::get('logs', [LogViewerController::class, 'index'])->middleware('can:view-logs')->name('log.viewer');
      
    • Events: Log custom events to the same file for centralized viewing:
      event(new CustomLogEvent('User action triggered'));
      
      Ensure the event’s toLog() method formats output compatibly with the viewer.
  5. API Access:

    • Extend the controller to return logs as JSON for internal tools:
      Route::get('logs/api', [LogViewerController::class, 'getLogsAsJson']);
      
      Add this method to the controller to return parsed logs:
      public function getLogsAsJson()
      {
          return response()->json($this->getLogs());
      }
      

Gotchas and Tips

Pitfalls

  1. Log File Permissions:

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

    • The package reads logs line-by-line, which may slow down for files >10MB.
    • Workaround: Use Laravel’s tail command or stream logs in chunks:
      $logs = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      array_slice($logs, -1000); // Show last 1000 lines
      
  3. Missing Published Files:

    • If you publish views/config but changes don’t reflect, clear the view cache:
      php artisan view:clear
      
  4. Namespace Conflicts:

    • The package uses \ViewerLogic\LogViewerLaravelProvider and \LogViewerLaravel\LogViewerController.
    • Tip: Verify autoloading with composer dump-autoload if routes fail.
  5. Log Rotation:

    • If logs are rotated (e.g., by laravel-logger), the viewer may not auto-detect new files.
    • Solution: Manually specify the log file path in config or extend the controller to scan for rotated files:
      $logFiles = glob(storage_path('logs/laravel*.log'));
      

Debugging

  1. Logs Not Showing:

    • Check if the log file exists at storage/logs/laravel.log.
    • Verify the route is registered (no typos in the controller namespace).
    • Enable debug mode in config/logviewer.php:
      'debug' => true,
      
      This may log errors to storage/logs/laravel.log.
  2. Custom View Not Loading:

    • Ensure the published view file exists at /resources/views/vendor/laravel-log-viewer/log.blade.php.
    • Check for Blade syntax errors by temporarily simplifying the template.
  3. Performance Issues:

    • Use Laravel’s Log::channel() to write logs to a dedicated file for the viewer:
      Log::channel('custom')->info('Test log');
      
      Then configure the viewer to use this channel’s log file.

Tips

  1. Environment-Specific Routes:

    • Disable the log viewer in production by wrapping the route in:
      if (app()->environment('local')) {
          Route::get('logs', [LogViewerController::class, 'index']);
      }
      
  2. Search Functionality:

    • Extend the controller to add a search parameter:
      public function index(Request $request)
      {
          $search = $request->query('search');
          $logs = $this->filterLogs($search);
          return view('vendor.laravel-log-viewer.log', ['logs' => $logs]);
      }
      
      Add a search input to the Blade view.
  3. Log Level Badges:

    • Use Tailwind CSS or custom CSS in the Blade view to style log levels:
      @foreach($logs as $log)
          <div class="p-2 {{ $log['level'] === 'error' ? 'bg-red-100' : 'bg-gray-100' }}">
              {{ $log['message'] }}
          </div>
      @endforeach
      
  4. GitHub Actions Integration:

    • Use the log viewer in CI/CD pipelines to debug failed jobs:
      - name: View logs on failure
        if: failure()
        run: curl http://localhost:8000/logs
      
      (Run the app locally in the pipeline with php artisan serve.)
  5. Extending for Multi-Tenant Apps:

    • Override the controller to dynamically set the log file based on the tenant:
      public function __construct()
      {
          $this->logFile = storage_path("logs/tenant-{$this->tenant->id}.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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui