Installation:
composer require zha/laravel-log-view
php artisan vendor:publish --provider="Zha\LaravelLogView\LogViewerServiceProvider" --tag="log-viewer-public"
public/vendor/laravel-log-view.Access Logs:
/log-viewer in your browser to view Laravel logs in a web interface.storage/logs/laravel.log.First Use Case:
Log Filtering:
error, warning, info).error logs from the last 24 hours to triage a critical issue.Integration with Existing Logs:
laravel.log, laravel-*.log). Ensure your config/logging.php channels are configured to write to these files.LogViewerServiceProvider to include additional paths.Programmatic Access:
LogViewer facade:
use Zha\LaravelLogView\Facades\LogViewer;
$logs = LogViewer::getLogs(['level' => 'error', 'days' => 1]);
Middleware/Route Protection:
/log-viewer route by adding middleware (e.g., auth or role:admin) in routes/web.php:
Route::middleware(['auth'])->get('/log-viewer', [LogViewerController::class, 'index']);
Log Rotation Handling:
laravel-log-rotate), ensure the package’s file scanner (Zha\LaravelLogView\LogViewer) is updated to handle rotated files. Override the getLogFiles() method in a service provider if needed.File Permissions:
www-data, nginx) has read access to storage/logs/. Run:
chmod -R 755 storage/logs/
chown -R www-data:www-data storage/logs/ # Adjust user/group as needed
Log File Encoding:
readLogFile() method in the LogViewer class to handle encoding.Large Log Files:
memory_limit temporarily during log viewing.limit parameter in the UI or API to paginate results:
LogViewer::getLogs(['limit' => 1000]);
Custom Log Paths:
storage/logs/laravel.log. To include additional paths (e.g., storage/logs/custom.log), extend the LogViewer class:
namespace App\Services;
use Zha\LaravelLogView\LogViewer as BaseLogViewer;
class CustomLogViewer extends BaseLogViewer {
protected function getLogFiles() {
return array_merge(parent::getLogFiles(), ['storage/logs/custom.log']);
}
}
Bind the custom class in a service provider:
$this->app->bind('logviewer', function () {
return new CustomLogViewer();
});
Caching Issues:
Check Published Assets:
ls public/vendor/laravel-log-view/
php artisan vendor:publish --tag="log-viewer-public" --force
LogViewer Facade Not Found:
config/app.php under providers. If using Laravel <8.x, manually add:
Zha\LaravelLogView\LogViewerServiceProvider::class,
Route Conflicts:
/log-viewer automatically. If this conflicts with existing routes, override the route in your RouteServiceProvider:
Route::get('/custom-log-viewer', [LogViewerController::class, 'index'])->name('log-viewer');
Custom Log Parsers:
LogViewer class to parse structured logs (e.g., JSON). Override the parseLogEntry() method:
protected function parseLogEntry(string $entry): array {
$data = json_decode($entry, true);
return [
'level' => $data['level'] ?? 'info',
'message' => $data['message'] ?? $entry,
'context' => $data['context'] ?? [],
];
}
Add Log Actions:
LogViewerController:
public function downloadLogs(Request $request) {
$logs = LogViewer::getLogs($request->all());
return response()->json($logs)->header('Content-Type', 'application/json');
}
Add a route and JavaScript to trigger this action.Multi-Tenant Logs:
getLogFiles() method to include tenant-specific paths:
protected function getLogFiles() {
$tenantId = auth()->user()->tenant_id;
return ["storage/logs/tenant_{$tenantId}/laravel.log"];
}
How can I help you explore Laravel packages today?