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

Nova Log Viewer Laravel Package

laravel/nova-log-viewer

View and search your Laravel application logs directly inside Laravel Nova. Nova Log Viewer adds a simple tool UI with real-time polling support, making it easy to inspect errors, stack traces, and recent log entries without leaving the admin panel.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Run the following command in your Laravel project directory:

    composer require laravel/nova-log-viewer
    
  2. Registration Register the tool in your NovaServiceProvider (app/Providers/NovaServiceProvider.php):

    public function tools()
    {
        return [
            \Laravel\Nova\LogViewer\LogViewer::make(),
            // ... other tools
        ];
    }
    
  3. First Use Case

    • Access the Log Viewer tool from the Nova sidebar.
    • View logs in real-time with auto-refresh (polling enabled by default).
    • Filter logs by level (e.g., error, debug) or keyword (e.g., authentication).

Implementation Patterns

Core Workflows

  1. Real-Time Log Monitoring

    • Enable polling for live updates (default interval: 10 seconds).
    • Customize polling interval via config:
      LogViewer::make()->withPollingInterval(5); // Refresh every 5 seconds
      
    • Useful for deployment monitoring or incident response.
  2. Debugging with Nova

    • Filter logs by level (e.g., error, warning) or search for keywords.
    • Copy log entries directly from Nova to share with teammates.
    • Link to resources (e.g., attach to a User resource for context):
      LogViewer::make()->viaResource(\App\Nova\User::class);
      
  3. Integration with Nova Features

    • Extend the tool by publishing its assets:
      php artisan vendor:publish --tag=nova-log-viewer
      
    • Customize UI by overriding the tool’s Blade view in resources/views/vendor/nova.
  4. Log Management

    • Clear logs via Laravel’s Log::clear() (if extended in a custom tool).
    • Configure log retention in .env:
      LOG_MAX_FILES=5
      

Advanced Patterns

  1. Conditional Tool Registration

    • Disable the tool in specific environments (e.g., production-only):
      public function tools()
      {
          if (app()->environment('production')) {
              return [
                  LogViewer::make(),
              ];
          }
          return [];
      }
      
  2. Log-Level-Specific Tools

    • Create separate tools for different log levels:
      LogViewer::make()->withLogLevel('error')->name('Error Logs'),
      LogViewer::make()->withLogLevel('debug')->name('Debug Logs'),
      
  3. Polling Optimization

    • Reduce polling frequency for high-traffic apps:
      LogViewer::make()->withPollingInterval(30); // Every 30 seconds
      
  4. RBAC for Log Access

    • Restrict log viewer access to specific roles (e.g., Admin):
      LogViewer::make()->authorize(function ($request) {
          return $request->user()->can('view logs');
      });
      

Gotchas and Tips

Common Pitfalls

  1. Polling Performance Issues

    • Problem: High polling intervals (e.g., 1s) can overload the server.
    • Fix: Set a reasonable default (e.g., 10s) and adjust based on log volume.
    • Debug: Check Nova’s nova.log for polling-related errors.
  2. Missing Log Files

    • Problem: Log files not appearing if Laravel’s log driver isn’t configured correctly.
    • Fix: Ensure LOG_CHANNEL in .env points to a valid driver (e.g., single, daily).
    • Debug: Verify log files exist in storage/logs/.
  3. Authentication Bypass

    • Problem: Unauthenticated users may see a 403 instead of a redirect.
    • Fix: Ensure the Authenticate middleware is properly registered (fixed in v1.2.0+).
  4. Large Log Files Slowing Nova

    • Problem: Nova UI lags when loading large log files.
    • Fix: Limit log retention or use a dedicated log management system for production.

Debugging Tips

  1. Check Nova’s Logs

    • Errors related to the log viewer appear in storage/logs/nova.log.
  2. Verify Tool Registration

    • Ensure the tool is registered in NovaServiceProvider::tools() and no syntax errors exist.
  3. Polling Debugging

    • Disable polling temporarily to test static log loading:
      LogViewer::make()->withoutPolling();
      
  4. Log Driver Issues

    • Test with the single log driver for simplicity:
      LOG_CHANNEL=single
      

Configuration Quirks

  1. Custom Log Paths

    • The package defaults to storage/logs/laravel.log. To change:
      LogViewer::make()->withLogPath('custom/path/to/logs');
      
  2. Environment-Specific Settings

    • Override polling interval per environment:
      $interval = app()->environment('local') ? 2 : 10;
      LogViewer::make()->withPollingInterval($interval);
      
  3. Tool Visibility

    • Hide the tool from specific Nova users:
      LogViewer::make()->visible(function ($request) {
          return $request->user()->isAdmin();
      });
      

Extension Points

  1. Custom Log Parsing

    • Extend the log viewer to parse custom log formats by overriding the resolveLogContents method in a custom tool class.
  2. Add Log Actions

    • Integrate with Nova’s action system to perform actions on log entries (e.g., "Replay Job"):
      LogViewer::make()->withActions([
          new ReplayJobAction,
      ]);
      
  3. Log Correlation

    • Enhance logs with request IDs or user contexts by modifying the log format in app/Providers/AppServiceProvider.php.
  4. Export Logs

    • Add a feature to export logs to a file or email by extending the tool’s handleExport method.

Performance Tips

  1. Reduce Polling Overhead

    • Use webhooks or Laravel Echo for real-time updates in high-traffic apps.
  2. Log File Rotation

    • Configure Laravel’s log rotation to manage file sizes:
      LOG_MAX_FILES=7
      LOG_LEVEL=debug
      
  3. Caching Log Metadata

    • Cache log file metadata (e.g., last modified) to reduce I/O operations during polling.

Security Considerations

  1. Log Access Control

    • Restrict log viewer access to authorized roles:
      LogViewer::make()->authorize(function ($request) {
          return $request->user()->hasRole(['admin', 'dev']);
      });
      
  2. Sensitive Data in Logs

    • Mask sensitive data (e.g., passwords, tokens) before logging using Laravel’s Log::withContext().
  3. Log File Permissions

    • Ensure Nova’s storage directory has proper permissions:
      chmod -R 755 storage/
      
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony