endanguyen/laravel-logviewer
Laravel 5/4.2 and Lumen log viewer. Install via Composer, register the service provider, and add a route to LogViewerController to browse local log files in the browser. No public assets or vendor routes; works with and without log rotation.
Installation
composer require endanguyen/laravel-logviewer
Publish the config (optional):
php artisan vendor:publish --provider="Endanguyen\LogViewer\LogViewerServiceProvider"
Register the Route
Add this to routes/web.php (or routes.php for Lumen):
Route::get('/logs', 'LogViewerController@index');
Or use the helper:
\LogViewer::routes();
First Use Case
Visit /logs in your browser. The package will:
storage/logs/laravel.log).error, info).Log Filtering
?level=error to filter logs by severity.LogViewer facade to support custom log channels:
LogViewer::addChannel('custom', 'path/to/custom.log');
Integration with Existing Log Channels
single channel. For multi-channel setups (e.g., stack or channels config), manually register channels in config/logviewer.php:
'channels' => [
'single' => storage_path('logs/laravel.log'),
'slack' => storage_path('logs/slack.log'),
],
Middleware Integration
Route::get('/logs', 'LogViewerController@index')->middleware('can:view-logs');
Customizing the View
php artisan vendor:publish --tag=logviewer.views
resources/views/vendor/logviewer/index.blade.php to add features like:
Programmatic Access
$logs = LogViewer::getLogs(['level' => 'error', 'limit' => 100]);
Log Rotation Conflicts
logrotate, the viewer may show truncated logs. Solution: Configure logviewer.php to include rotated logs:
'include_rotated' => true,
channels array.Permission Issues
www-data) has read access to storage/logs/:
chmod -R 755 storage/logs/
chown -R www-data:www-data storage/logs/
Large Log Files
config/logviewer.php:
'max_log_size' => 50, // MB
?limit=500 in the URL to paginate results.Lumen Compatibility
Log facade. Workaround: Manually bind the facade in bootstrap/app.php:
$app->bind('log', function () {
return new \Illuminate\Support\Facades\Log;
});
Verify Log Paths
Check config/logviewer.php for correct paths. Use LogViewer::getChannels() to debug:
dd(LogViewer::getChannels());
Clear Cache After config changes, clear Laravel’s cache:
php artisan config:clear
php artisan view:clear
Log Level Case Sensitivity Filtering is case-insensitive, but ensure consistency in config:
// Valid:
?level=ERROR
?level=error
Custom Log Parsers
Extend the LogEntry model to parse structured logs (e.g., JSON):
namespace Endanguyen\LogViewer;
class CustomLogEntry extends LogEntry
{
public function parseMessage($message)
{
return json_decode($message, true);
}
}
Register the custom parser in LogViewerServiceProvider.
Add Search Functionality Override the controller to support search:
public function index(Request $request)
{
$query = $request->input('q');
$logs = LogViewer::searchLogs($query, $request->all());
return view('vendor.logviewer.index', compact('logs'));
}
Export Logs Add a route to export logs as JSON/CSV:
Route::get('/logs/export', function () {
return response()->json(LogViewer::getLogs())->header('Content-Type', 'application/json');
});
How can I help you explore Laravel packages today?