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

mannysoft/laravel-log-viewer

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require rap2hpoutre/laravel-log-viewer
    

    Add the service provider to config/app.php under providers:

    Mannysoft\LogViewer\LogViewerServiceProvider::class,
    
  2. Publish Config (Optional) Publish the default config to customize log paths or filters:

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

    Edit config/log-viewer.php if needed.

  3. First Use Case Add a route to access the log viewer (e.g., in routes/web.php):

    use Mannysoft\LogViewer\Controllers\LogViewerController;
    
    Route::get('/logs', [LogViewerController::class, 'index']);
    

    Visit /logs to see a real-time log viewer interface.


Implementation Patterns

Core Workflows

  1. Log Filtering

    • Filter logs by level (e.g., error, info) via URL query:
      /logs?level=error
      
    • Filter by date range (YYYY-MM-DD format):
      /logs?from=2024-01-01&to=2024-01-31
      
    • Filter by search term (case-insensitive):
      /logs?search=authentication
      
  2. Integration with Existing Logs

    • Works seamlessly with Laravel’s default log channels (single file, daily, syslog, etc.).
    • Supports log rotation (e.g., single, daily) without manual configuration.
  3. Customizing Log Paths

    • Override default log paths in config/log-viewer.php:
      'paths' => [
          storage_path('logs/custom-laravel.log'),
          storage_path('logs/another-service.log'),
      ],
      
  4. Access Control

    • Restrict access via middleware (e.g., auth or custom middleware):
      Route::get('/logs', [LogViewerController::class, 'index'])->middleware('can:view-logs');
      
  5. Programmatic Access

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

Advanced Patterns

  1. Extending Log Parsing

    • Override log parsing logic by binding a custom LogParser:
      $this->app->bind('logviewer.parser', function () {
          return new CustomLogParser();
      });
      
    • Implement Mannysoft\LogViewer\Contracts\LogParser to handle custom log formats.
  2. Adding Log Actions

    • Extend the controller to add actions (e.g., log deletion):
      public function deleteLogs(Request $request) {
          $this->logViewer->deleteLogs($request->input('files'));
          return back()->with('success', 'Logs deleted');
      }
      
  3. Real-Time Updates

    • Use Laravel Echo/Pusher to push log updates to clients (requires frontend integration).
  4. Log Export

    • Add a download endpoint to export logs as JSON/CSV:
      public function exportLogs(Request $request) {
          return response()->streamDownload(function () {
              echo json_encode($this->logViewer->getLogs($request->all()));
          }, 'logs.json');
      }
      

Gotchas and Tips

Common Pitfalls

  1. Permission Issues

    • Ensure the web server user (e.g., www-data, nginx) has read access to log files:
      chmod -R 755 storage/logs/
      chown -R www-data:www-data storage/logs/
      
    • If using Docker, ensure volumes are mounted correctly.
  2. Log Rotation Conflicts

    • If logs are rotated (e.g., daily), the viewer may not show new logs until the next rotation.
    • Fix: Use logrotate with copytruncate or configure Laravel’s Log::rotate() to avoid gaps.
  3. Large Log Files

    • Performance degrades with >10MB log files. Consider:
      • Splitting logs into smaller files.
      • Using tail -f for real-time streaming (requires custom implementation).
  4. Caching Headaches

    • Avoid caching the log viewer route (e.g., Cache::remember()). Logs are dynamic!
  5. Middleware Conflicts

    • If using trustedproxy or throttle, ensure the log viewer route is excluded:
      Route::middleware(['web', 'log-viewer'])->group(function () {
          Route::get('/logs', [LogViewerController::class, 'index']);
      });
      

Debugging Tips

  1. Check Log Paths

    • Verify config/log-viewer.php paths match your actual log locations:
      dd(config('log-viewer.paths'));
      
  2. Enable Debug Mode

    • Set APP_DEBUG=true to see detailed errors if the viewer loads blank.
  3. Log Parser Issues

    • If logs appear garbled, check the LogParser implementation:
      $this->app->singleton('logviewer.parser', function () {
          return new \Mannysoft\LogViewer\Parsers\MonologParser();
      });
      
  4. File Permissions

    • Test log access manually:
      tail -n 10 storage/logs/laravel.log
      
    • If this fails, fix permissions before debugging further.

Extension Points

  1. Custom Log Formats

    • Override the default Monolog parser for non-standard logs (e.g., JSON, custom delimiters).
  2. UI Customization

    • Extend the Blade template (resources/views/vendor/log-viewer/index.blade.php) to add:
      • Syntax highlighting for log levels.
      • Collapsible log entries.
      • Custom CSS/JS.
  3. API Endpoint

    • Convert the controller to a JSON API for frontend frameworks:
      public function apiLogs(Request $request) {
          return response()->json($this->logViewer->getLogs($request->all()));
      }
      
  4. Log Anonymization

    • Filter sensitive data (e.g., passwords, tokens) before displaying logs:
      $this->app->afterResolving('logviewer.parser', function ($parser) {
          $parser->setFilter(function ($log) {
              return preg_replace('/password=[^&]+/', 'password=***', $log);
          });
      });
      
  5. Alerting Integration

    • Hook into log events to trigger alerts (e.g., Slack notifications for error logs):
      LogViewer::listen(function ($logs) {
          if (collect($logs)->contains('authentication failed')) {
              // Send alert
          }
      });
      
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.
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
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours