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

dipeshshihora/laravel-log-viewer

Simple log viewer for Laravel 9–12 and Lumen. Install via Composer, add a /logs route to LogViewerController, and browse storage logs with a plain HTML UI. Optional publishing for views/config; supports patterns, multiple paths, paging, and max file size limits.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require dipeshshihora/laravel-log-viewer
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Dipeshshihora\LogViewer\LogViewerServiceProvider"
    
  2. First Use Case:

    • Register the middleware in app/Http/Kernel.php:
      'web' => [
          \Dipeshshihora\LogViewer\Middleware\LogViewerMiddleware::class,
      ],
      
    • Access logs via /logs route (default) or configure a custom route in config/log-viewer.php.
  3. Quick View:

    • Visit /logs in your browser to see a filtered, searchable log interface.
    • Defaults to showing storage/logs/laravel.log (adjustable in config).

Implementation Patterns

Core Workflows

  1. Log Filtering:

    • Use query parameters to filter logs:
      /logs?level=error&search=authentication
      
    • Supported filters: level (debug, info, warning, error, critical), search, from, to.
  2. Custom Log Files:

    • Override the default log path in config/log-viewer.php:
      'log_file' => storage_path('logs/custom.log'),
      
    • Support multiple log files via log_files array:
      'log_files' => [
          storage_path('logs/laravel.log'),
          storage_path('logs/custom.log'),
      ],
      
  3. Integration with Existing Routes:

    • Protect the log viewer route with middleware (e.g., auth):
      Route::get('/logs', [LogViewerController::class, 'index'])->middleware('auth');
      
    • Extend the controller to add custom logic (e.g., log masking):
      namespace App\Http\Controllers;
      use Dipeshshihora\LogViewer\Controllers\LogViewerController as BaseController;
      
      class LogViewerController extends BaseController {
          public function index() {
              // Pre-process logs (e.g., mask sensitive data)
              $logs = parent::index();
              return view('log-viewer::index', compact('logs'));
          }
      }
      
  4. Blade Customization:

    • Publish views and override templates:
      php artisan vendor:publish --tag=log-viewer-views
      
    • Modify resources/views/vendor/log-viewer/index.blade.php to add columns, buttons, or styling.
  5. API Access:

    • Use the LogViewerFacade to fetch logs programmatically:
      use Dipeshshihora\LogViewer\Facades\LogViewer;
      
      $logs = LogViewer::getLogs(['level' => 'error']);
      

Gotchas and Tips

Pitfalls

  1. Large Log Files:

    • Performance degrades with log files >100MB. Use log_max_lines in config to limit results:
      'log_max_lines' => 5000,
      
    • For production, consider streaming logs or using a dedicated log management tool.
  2. Permission Issues:

    • Ensure the web server user (e.g., www-data) has read access to storage/logs/:
      chmod -R 755 storage/logs/
      
    • If using Docker, bind-mount the logs directory with proper permissions.
  3. Log Rotation:

    • The package doesn’t handle log rotation. Use Laravel’s built-in log rotation (config/logging) or a tool like logrotate.
  4. Monolog-Specific Quirks:

    • Logs must be in Monolog’s JSON format. If using custom log handlers, ensure compatibility.
    • For non-JSON logs, pre-process them or use a different package.
  5. Route Conflicts:

    • The default /logs route may conflict with existing routes. Always check routes/web.php and adjust the route in config/log-viewer.php:
      'route' => 'admin/logs',
      

Debugging Tips

  1. Log Format Issues:

    • If logs don’t display, verify the format with:
      tail -n 10 storage/logs/laravel.log | jq
      
    • Ensure logs are JSON-encoded (Monolog’s default).
  2. Middleware Not Triggering:

    • Check if the middleware is registered in app/Http/Kernel.php and the route is accessible.
    • Test with a simple route to isolate the issue:
      Route::get('/test-log-viewer', function () {
          return LogViewer::getLogs();
      });
      
  3. Custom Controller Overrides:

    • If extending the controller, ensure you’re calling parent methods (e.g., parent::index()) to retain default functionality.
  4. Caching:

    • The package may cache log queries. Clear cache if logs aren’t updating:
      php artisan cache:clear
      php artisan view:clear
      

Extension Points

  1. Custom Log Parsers:

    • Extend the LogParser class to support non-JSON logs:
      namespace App\Services;
      use Dipeshshihora\LogViewer\Services\LogParser as BaseParser;
      
      class CustomLogParser extends BaseParser {
          public function parseLine(string $line): array {
              // Custom parsing logic
              return $parsedData;
          }
      }
      
    • Bind the parser in a service provider:
      $this->app->bind(LogParser::class, CustomLogParser::class);
      
  2. Adding Metadata:

    • Extend the log view to include additional metadata (e.g., user context):
      // In a middleware or service
      Log::withContext(['user_id' => auth()->id()])->error('Test error');
      
    • Update the Blade template to display context data.
  3. Export Functionality:

    • Add a download button to export logs as JSON/CSV:
      // In LogViewerController
      public function export(Request $request) {
          $logs = LogViewer::getLogs($request->all());
          return response()->json($logs)->header('Content-Type', 'application/json');
      }
      
    • Add a route and JavaScript to trigger the export.
  4. Real-Time Updates:

    • Use Laravel Echo/Pusher to stream new logs in real-time:
      // Public/js/app.js
      Echo.channel('log-channel')
          .listen('LogEvent', (e) => {
              // Append new log to the UI
          });
      
    • Emit events in a log observer or listener.
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.
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
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager