gopalindians/laravel-log-viewer
Lightweight log viewer for Laravel 6–13 and Lumen. Install via Composer and add a single route to LogViewerController—no public assets or vendor routes required. Works with rotated logs and supports view customization via publishing.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require gopalindians/laravel-log-viewer
Verify the package is listed in composer.json under require.
Publish Config (Optional):
php artisan vendor:publish --provider="Rap2hpoutre\LaravelLogViewer\LogViewerServiceProvider"
This creates a config/log-viewer.php file for customization (e.g., log paths, filters).
Route Integration:
Add the route in routes/web.php (or routes/api.php for API access):
Route::get('/logs', [\Rap2hpoutre\LaravelLogViewer\Controller\LogViewerController::class, 'index']);
For Lumen, register the route in bootstrap/app.php:
$app->router->get('/logs', 'Rap2hpoutre\LaravelLogViewer\Controller\LogViewerController@index');
First Use Case:
Visit /logs in your browser. The package automatically detects Laravel’s default log paths (storage/logs/laravel.log and rotated files like laravel-2024-01-01.log). No additional configuration is needed for basic usage.
Log Filtering:
date_from and date_to query parameters to filter logs by timestamp:
/logs?date_from=2024-01-01&date_to=2024-01-31
error, info) via level parameter:
/logs?level=error
search parameter to find specific entries:
/logs?search=Exception
Custom Log Paths:
Override default log paths in config/log-viewer.php:
'log_paths' => [
storage_path('logs/custom-app.log'),
storage_path('logs/third-party.log'),
],
Integration with Middleware:
Restrict access to logs using Laravel middleware (e.g., auth or admin):
Route::get('/logs', [LogViewerController::class, 'index'])->middleware('can:view-logs');
API Access: Return logs as JSON for API consumption by modifying the controller or creating a dedicated endpoint:
Route::get('/api/logs', [LogViewerController::class, 'apiIndex']);
Extend the controller to support JSON responses:
public function apiIndex(Request $request) {
$logs = $this->getLogs($request);
return response()->json($logs);
}
Log Rotation Handling:
The package automatically handles rotated log files (e.g., laravel-*.log). No manual intervention is required.
Log Anonymization:
Use Laravel’s Log::shouldNotLog() or middleware to mask sensitive data before it reaches the logs. The viewer will display the sanitized output.
Log Archiving:
Combine with Laravel’s Log::archive() or a cron job to move old logs to a separate directory (e.g., storage/logs/archive/). Update log_paths to include the archive path.
Real-Time Logs: For real-time log streaming, use Laravel Echo or a WebSocket solution (e.g., Pusher) to push new log entries to clients. The viewer itself is not real-time but can be extended to support it.
Permission Issues:
www-data, nginx) has read access to storage/logs/.chmod -R 755 storage/logs/
chown -R www-data:www-data storage/logs/ # Adjust user/group as needed
Missing Log Files:
config/logging.php. The package relies on the default single or daily channels./var/log/laravel.log). Update log_paths in the config.Rotated Logs Not Displayed:
laravel-2024-01-01.log), but if they’re missing, ensure Laravel’s log rotation is configured correctly in config/logging.php:
'daily' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 15, // Retain logs for 15 days
],
Performance with Large Logs:
date_from/date_to to reduce the dataset.Log::getMonolog() to stream logs line-by-line (requires custom controller logic).Caching Issues:
Check Controller Output: Temporarily add debug output to the controller to verify log paths and filters:
public function index(Request $request) {
dd($this->getLogPaths()); // Inspect available log paths
dd($request->all()); // Verify query parameters
}
Log Viewer Configuration: Dump the config to ensure settings are loaded:
dd(config('log-viewer'));
File Existence: Verify log files exist and are readable:
ls -la storage/logs/
cat storage/logs/laravel.log # Check if logs are being written
Customize the View: Publish the view file and modify it:
php artisan vendor:publish --tag=log-viewer-views
Edit resources/views/vendor/log-viewer/index.blade.php to add features like:
Add Log Actions: Extend the controller to add functionality like:
public function clearLogs() {
foreach ($this->getLogPaths() as $path) {
if (file_exists($path)) unlink($path);
}
return back()->with('status', 'Logs cleared!');
}
public function downloadLogs(Request $request) {
$logs = $this->getLogs($request);
return response()->streamDownload(function () use ($logs) {
echo implode("\n", $logs);
}, 'laravel-logs.txt');
}
Integrate with Laravel Debugbar: Use the Laravel Debugbar package to display recent logs in the debug toolbar:
Debugbar::info('Logs', [
'recent' => Log::getMonolog()->getLogs(),
]);
Localization: Localize the viewer’s UI by publishing and translating the language file:
php artisan vendor:publish --tag=log-viewer-lang
Edit resources/lang/en/log-viewer.php.
Security Hardening:
Gate::define('view-logs', function ($user) {
return $user->isAdmin();
});
Route::middleware(['throttle:60,1'])->group(function () {
Route::get('/logs', [LogViewerController::class, 'index']);
});
---
How can I help you explore Laravel packages today?