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

kahovskaia/laravel-log-viewer

Lightweight Laravel log viewer package for browsing application log files from within your app. Helps locate, filter, and inspect recent entries during development or debugging without leaving the Laravel environment.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require kahovskaia/laravel-log-viewer
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Kahovskaia\LogViewer\LogViewerServiceProvider"
    
  2. Basic Usage The package provides a facade (LogViewer) to read log files. Start by configuring the log paths in config/log-viewer.php:

    'paths' => [
        storage_path('logs/laravel.log'),
        storage_path('logs/debug.log'),
    ],
    
  3. First Use Case Fetch and display logs in a controller or blade view:

    use Kahovskaia\LogViewer\Facades\LogViewer;
    
    $logs = LogViewer::getLogs();
    return view('logs.view', compact('logs'));
    

Implementation Patterns

Core Workflows

  1. Reading Logs Fetch logs with optional filtering:

    // Get all logs
    $logs = LogViewer::getLogs();
    
    // Filter by date range (YYYY-MM-DD)
    $logs = LogViewer::getLogs('2023-01-01', '2023-01-31');
    
    // Filter by log level (e.g., 'error', 'debug')
    $logs = LogViewer::getLogs(null, null, 'error');
    
  2. Integration with Blade Views Create a dedicated view (resources/views/logs/view.blade.php):

    @foreach($logs as $log)
        <div class="log-entry">
            <strong>{{ $log['datetime'] }}</strong>
            <span class="level">{{ $log['level'] }}</span>
            <p>{{ $log['message'] }}</p>
        </div>
    @endforeach
    
  3. API Endpoint for Logs Expose logs via an API route:

    Route::get('/api/logs', function () {
        return response()->json(LogViewer::getLogs());
    });
    

Advanced Patterns

  1. Custom Log Parsing Extend the package by overriding the LogParser class (located in src/LogParser.php):

    namespace App\Services;
    
    use Kahovskaia\LogViewer\LogParser as BaseLogParser;
    
    class CustomLogParser extends BaseLogParser
    {
        protected function parseLine($line)
        {
            // Custom parsing logic
            return parent::parseLine($line);
        }
    }
    

    Bind the custom parser in AppServiceProvider:

    public function register()
    {
        $this->app->bind(
            \Kahovskaia\LogViewer\Contracts\LogParser::class,
            \App\Services\CustomLogParser::class
        );
    }
    
  2. Log Tailing (Real-Time) Implement a real-time log viewer using Laravel Echo/Pusher or a simple AJAX poll:

    // Example AJAX polling
    function pollLogs() {
        fetch('/api/logs')
            .then(response => response.json())
            .then(logs => {
                // Update UI with new logs
                document.getElementById('logs-container').innerHTML = logs.map(log => `<div>${log.message}</div>`).join('');
            });
        setTimeout(pollLogs, 5000); // Poll every 5 seconds
    }
    pollLogs();
    
  3. Log Search Functionality Add search to your Blade view:

    // Controller
    $searchTerm = request('search');
    $logs = LogViewer::getLogs(null, null, null, $searchTerm);
    
    // Blade
    <input type="text" name="search" placeholder="Search logs...">
    <button>Search</button>
    

Gotchas and Tips

Pitfalls

  1. Log File Rotation

    • Laravel logs rotate daily by default. Ensure LogViewer paths include all rotated logs (e.g., laravel.log, laravel-2023-01-01.log).
    • Fix: Use wildcards in config:
      'paths' => [storage_path('logs/laravel-*.log')],
      
  2. Performance with Large Logs

    • Parsing large log files can be slow. Avoid fetching all logs at once in production.
    • Fix: Implement pagination or limit results:
      $logs = LogViewer::getLogs(null, null, null, null, 50); // Limit to 50 entries
      
  3. Custom Log Formats

    • The package assumes Laravel’s default log format. Custom log formats (e.g., JSON, syslog) may break parsing.
    • Fix: Extend LogParser (as shown above) or pre-process logs.
  4. Permissions

    • Ensure the web server user (e.g., www-data) has read access to log files:
      chmod 644 storage/logs/*
      chown -R www-data:www-data storage/logs/
      

Debugging Tips

  1. Check Parsed Logs Temporarily dump parsed logs to debug:

    $rawLogs = LogViewer::readLogFiles();
    dd($rawLogs); // Inspect raw log content
    
  2. Log Parser Errors If logs aren’t parsing correctly, verify the parseLine method in LogParser matches your log format. Example debug line:

    protected function parseLine($line)
    {
        if (preg_match('/\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\] (\w+)\. (\d+)\. (.*)/', $line, $matches)) {
            return [
                'datetime' => $matches[1],
                'level' => $matches[2],
                'context' => $matches[4],
                'message' => $matches[4],
            ];
        }
        return null; // Return null for unparseable lines
    }
    
  3. Missing Logs

    • If logs aren’t appearing, verify:
      • The log path in config/log-viewer.php is correct.
      • Laravel’s LOG_CHANNEL in .env matches the log file location.
      • The log file isn’t being overwritten by another process.

Extension Points

  1. Add Log Levels Extend the supported log levels in LogViewer:

    // In a service provider
    $this->app->extend('log-viewer', function ($viewer) {
        $viewer->addLogLevel('custom', 'Custom Level');
        return $viewer;
    });
    
  2. Custom Storage Backend Override the default file-based log reader to fetch logs from a database or external service:

    namespace App\Services;
    
    use Kahovskaia\LogViewer\Contracts\LogReader;
    
    class DatabaseLogReader implements LogReader
    {
        public function read()
        {
            return DB::table('logs')->get()->toArray();
        }
    }
    

    Bind it in AppServiceProvider:

    $this->app->bind(
        \Kahovskaia\LogViewer\Contracts\LogReader::class,
        \App\Services\DatabaseLogReader::class
    );
    
  3. Log Anonymization Sanitize sensitive data in logs before displaying them:

    $logs = LogViewer::getLogs();
    $sanitizedLogs = array_map(function ($log) {
        $log['message'] = preg_replace('/[a-zA-Z0-9]{8,}/', '[REDACTED]', $log['message']);
        return $log;
    }, $logs);
    
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.
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
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope