Installation:
composer require sn0rk64/laravel-log-viewer
(Note: The package is a fork of rap2hpoutre/laravel-log-viewer; ensure you’re using the correct namespace in your code.)
Register Service Provider (Laravel):
Add to config/app.php:
Sn0rk64\LaravelLogViewer\LaravelLogViewerServiceProvider::class,
Add Route (Laravel):
In routes/web.php:
Route::get('logs', '\Sn0rk64\LaravelLogViewer\LogViewerController@index');
Access Logs:
Visit /logs in your browser. The viewer will display logs from storage/logs/laravel.log by default.
error, warning) to isolate problems.Log Filtering:
Use the built-in log level filters (e.g., debug, info, error) to narrow down logs. Example URL:
/logs?level=error
(Supports multiple levels via comma-separated values: ?level=error,warning.)
JSON Logs: The package automatically formats JSON log entries for readability. No manual parsing required.
Lumen Support:
Follow the Lumen-specific setup in the README (namespace grouping in bootstrap/app.php and routes.php).
Custom Log Paths: Override the default log path by publishing the config:
php artisan vendor:publish --provider="Sn0rk64\LaravelLogViewer\LaravelLogViewerServiceProvider" --tag="config"
Then modify config/log-viewer.php to set log_file.
Middleware Protection: Restrict access to logs in production by wrapping the route in middleware:
Route::get('logs', function () {
if (app()->environment('production')) {
abort(403);
}
return \Sn0rk64\LaravelLogViewer\LogViewerController::index();
});
Log Rotation Compatibility:
Works seamlessly with Laravel’s log rotation (e.g., laravel.log.2023_01_01). No additional setup needed.
Custom Views: Publish the view files for modification:
php artisan vendor:publish --provider="Sn0rk64\LaravelLogViewer\LaravelLogViewerServiceProvider" --tag="views"
Override resources/views/vendor/log-viewer/index.blade.php to add custom UI elements (e.g., timestamps, user context).
Namespace Mismatch:
The package uses Sn0rk64\LaravelLogViewer (not Rap2hpoutre). Ensure all routes/controllers reference the correct namespace.
Fix: Update routes and service provider registration to match the forked package.
Log File Permissions:
If logs fail to load, verify the web server user (e.g., www-data) has read access to storage/logs/.
Fix: Run:
chmod -R 755 storage/logs/
chown -R www-data:www-data storage/logs/
JSON Parsing Errors:
Malformed JSON in logs may break the pretty-printing. The package silently skips invalid entries.
Debug: Check raw logs via tail -f storage/logs/laravel.log for syntax issues.
Route Cache Conflicts: After changing routes, clear the route cache:
php artisan route:clear
Log Level Filtering:
Use ?level=* to show all logs (default) or ?level=debug,info for specific levels. Debug logs are hidden by default in production.
Log Truncation:
The viewer loads logs in chunks (configurable via config/log-viewer.php). Adjust max_lines to control pagination.
Custom Log Channels:
To view logs from non-default channels (e.g., single or daily), extend the LogViewerController:
use Monolog\Logger;
public function index() {
$logs = Logger::getMonolog()->channels['custom_channel']->getLogs();
// Custom logic to render logs
}
Add Log Context: Extend the view to include request context (e.g., user ID, IP) by modifying the published blade template:
@if(request()->user())
<div class="context">User: {{ request()->user()->name }}</div>
@endif
Log Search:
Implement a search feature by overriding the controller’s index() method to filter logs by keyword:
public function index() {
$search = request('search');
$logs = collect($this->getLogs())
->filter(fn($log) => $search && stripos($log['message'], $search) !== false);
return view('log-viewer::index', ['logs' => $logs]);
}
Log Export: Add a download button to export logs as JSON:
Route::get('logs/export', [LogViewerController::class, 'export']);
In the controller:
public function export() {
return response()->json($this->getLogs());
}
How can I help you explore Laravel packages today?