kolamitin/laravel-log-viewer
Lightweight Laravel (and Lumen) log viewer. Install via Composer, register the service provider, and add a route to LogViewerController to browse log files in the browser. No public assets or vendor routes; works with rotated logs.
Installation:
composer require rap2hpoutre/laravel-log-viewer
(Note: The README mentions kolamitin/laravel-log-viewer, but the package is actually rap2hpoutre/laravel-log-viewer per the excerpt.)
Publish Config (Optional):
php artisan vendor:publish --provider="Rap2hpoutre\LaravelLogViewer\LogViewerServiceProvider"
(Default config is minimal; publish only if customizing paths or filters.)
Add Route:
// routes/web.php
Route::get('/logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index');
(Use middleware like auth or admin to restrict access.)
First Use Case:
/logs in your browser to view formatted logs.error, info), or search content.Log Filtering:
emergency, alert, critical, error, warning, notice, info, or debug.Integration with Existing Logs:
storage/logs/laravel.log) or rotated logs (e.g., laravel-*.log).Customization:
php artisan vendor:publish --tag=log-viewer-views) and modify:
resources/views/vendor/laravel-log-viewer/index.blade.php (main layout).resources/views/vendor/laravel-log-viewer/partials/entry.blade.php (per-log entry styling).LogViewerController or creating a middleware to pre-process logs.Middleware Integration:
Route::get('/logs', '\Rap2hpoutre\LaravelLogViewer\LogViewerController@index')->middleware('can:view-logs');
Programmatic Access:
LogViewer facade to fetch logs programmatically:
use Rap2hpoutre\LaravelLogViewer\Facades\LogViewer;
$logs = LogViewer::getLogs()->filterByLevel('error')->get();
Log Rotation Handling:
laravel-2023-10-01.log). No config needed for standard Laravel log rotation.Custom Log Paths:
config/log-viewer.php:
'log_path' => storage_path('logs/custom'),
Localization:
php artisan vendor:publish --tag=log-viewer-lang
Performance:
limit to the controller:
public function index()
{
return view('vendor.laravel-log-viewer.index', [
'logs' => LogViewer::getLogs()->limit(1000)->get(),
]);
}
Outdated Package:
spatie/laravel-log-viewer if critical.Log File Permissions:
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
Rotated Logs Not Showing:
logrotate), ensure filenames match Laravel’s default pattern (laravel-YYYY-MM-DD.log).LogViewer service to support custom patterns.Memory Issues:
limit() method or paginate results:
$logs = LogViewer::getLogs()->limit(500)->paginate(50);
CSRF Token Errors:
index.blade.php:
@csrf
Log File Path Issues:
log_path in config/log-viewer.php points to the correct directory.log_path vs. path).Blank Page or 500 Error:
APP_DEBUG=true in .env) to see detailed errors.storage/logs directory (run php artisan storage:link).Search Not Working:
' or ").str_contains instead of str_contains in the controller.Custom Log Formats:
entry.blade.php partial to support structured logs (e.g., JSON):
// In your custom entry.blade.php
@php
$logEntry = json_decode($entry['message'], true) ?? $entry['message'];
@endphp
<pre>{{ print_r($logEntry, true) }}</pre>
Add Context Links:
// In LogViewerController
$logs = LogViewer::getLogs()->map(function ($log) {
$log['message'] = str_replace(
'user_id:123',
'<a href="/users/123">user_id:123</a>',
$log['message']
);
return $log;
});
API Endpoint:
Route::get('/api/logs', function () {
return response()->json(LogViewer::getLogs()->get());
});
Log Anonymization:
// In a middleware or service provider
$logMessage = preg_replace('/"password":"[^"]+"/', '"password":"[FILTERED]"', $log['message']);
How can I help you explore Laravel packages today?