PHP code example of mikailfaruqali / log-viewer

1. Go to this page and download the library: Download mikailfaruqali/log-viewer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mikailfaruqali / log-viewer example snippets




return [
    /*
    |--------------------------------------------------------------------------
    | Log Viewer Route Path
    |--------------------------------------------------------------------------
    | This is the URI path where the log viewer will be accessible.
    | Default: 'logs' (accessible at /logs)
    |
    */
    'route-path' => 'logs',

    /*
    |--------------------------------------------------------------------------
    | Log Viewer Route Middleware
    |--------------------------------------------------------------------------
    | These middleware will be assigned to every route in the package.
    | You can add your own middleware here to restrict access.
    | Default: ['web', 'auth'] - 

'route-path' => 'admin/logs', // Accessible at /admin/logs

'middleware' => ['web', 'auth', 'admin'], // Add admin middleware

'middleware' => ['web', 'auth', 'can:view-logs'], // Laravel Gates/Policies

// app/Http/Middleware/LogViewerAccess.php


namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class LogViewerAccess
{
    public function handle(Request $request, Closure $next)
    {
        if (!auth()->user()->hasRole('admin')) {
            abort(403, 'Access denied to log viewer');
        }

        return $next($request);
    }
}

// config/snawbar-log-viewer.php
'middleware' => ['web', 'auth', App\Http\Middleware\LogViewerAccess::class],

// app/Providers/AuthServiceProvider.php
Gate::define('view-logs', function ($user) {
    return $user->hasPermission('view-logs');
});

// config/snawbar-log-viewer.php
'middleware' => ['web', 'auth', 'can:view-logs'],

// config/snawbar-log-viewer.php
'middleware' => app()->environment('production') 
    ? ['web', 'auth', 'admin'] 
    : ['web'],

use Monolog\Formatter\JsonFormatter;

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['single'],
    ],
    'single' => [
        'driver'    => 'single',
        'path'      => storage_path('logs/laravel.log'),
        'level'     => env('LOG_LEVEL', 'debug'),
        'formatter' => JsonFormatter::class,
    ],
],

// Recommended production middleware
'middleware' => [
    'web', 
    'auth', 
    'role:admin',           // Role-based access
    'throttle:60,1',        // Rate limiting
    'verified',             // Email verification
],

// In a custom middleware
public function handle($request, Closure $next)
{
    $allowedIps = ['192.168.1.100', '10.0.0.50'];
    
    if (!in_array($request->ip(), $allowedIps)) {
        abort(403);
    }
    
    return $next($request);
}

// In your middleware
Log::info('Log viewer accessed', [
    'user' => auth()->user()->email,
    'ip' => $request->ip(),
    'user_agent' => $request->userAgent(),
]);

// In your .env file
LOG_LEVEL=debug

// This will log parsing errors and other debug information

// In a scheduled command
$this->command('log:clear --days=30'); // Keep 30 days of logs
bash
php artisan vendor:publish --provider="Snawbar\LogViewer\LogViewerServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Snawbar\LogViewer\LogViewerServiceProvider" --tag="views"

┌─────────────────────────────────────────────────────────────┐
│ [☰] Log Viewer Dashboard                                    │
├─────────────────────────────────────────────────────────────┤
│ 📁 Log Files        │ 📋 Viewing: laravel-2024-01-15.log   │
│                     │                                       │
│ laravel-2024-01-15  │ [ERROR] 2024-01-15 10:30:45         │
│ laravel-2024-01-14  │ Undefined variable: user             │
│ laravel-2024-01-13  │ ▼ Click to expand stack trace        │
│                     │                                       │
│ [🗑️] Delete buttons │ [INFO] 2024-01-15 10:25:12          │
│                     │ User login successful                 │
│                     │                                       │
│                     │ [WARNING] 2024-01-15 09:15:33        │
│                     │ Deprecated function usage            │
└─────────────────────────────────────────────────────────────┘