composer require sdtech/log-viewer-laravel
php artisan vendor:publish --provider="SDTech\LogViewer\LogViewerServiceProvider" --tag="config"
php artisan vendor:publish --provider="SDTech\LogViewer\LogViewerServiceProvider" --tag="migrations"
php artisan migrate
routes/web.php):
Route::middleware(['web', 'auth'])->group(function () {
Route::get('/logs', [\SDTech\LogViewer\Http\Controllers\LogViewerController::class, 'index']);
});
/logs (requires auth middleware). The package provides a filtered, searchable UI for all stored logs.Exception, debug).Log Storage Integration
config/logging.php:
'channels' => [
'log_viewer' => [
'driver' => 'custom',
'via' => \SDTech\LogViewer\LogChannels\LogViewerChannel::class,
],
],
config/app.php under logging.default.Filtering and Search
level, date range, context). For custom filters, extend the controller:
public function index(Request $request)
{
$logs = \SDTech\LogViewer\Models\Log::query()
->when($request->has('custom_filter'), function ($query) use ($request) {
return $query->where('message', 'like', '%' . $request->custom_filter . '%');
})
->paginate(50);
return view('log-viewer::index', compact('logs'));
}
Log Management
LogViewerController method:
public function clear(Request $request)
{
\SDTech\LogViewer\Models\Log::truncate();
return back()->with('success', 'Logs cleared!');
}
Route::get('/logs/download', function () {
return \SDTech\LogViewer\Http\Controllers\LogViewerController::downloadLogs();
});
Real-Time Monitoring
Echo.channel('logs')
.listen('LogStored', (e) => {
// Append new log to UI dynamically
});
(Requires extending the package to emit events.)Performance with Large Logs
created_at, level, and context columns:
Schema::table('logs', function (Blueprint $table) {
$table->index('level');
$table->index('context');
});
->paginate(50)) and lazy-load logs.Log Retention
php artisan schedule:run
Add to app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command(LogViewerPurgeCommand::class)->daily();
}
Middleware Conflicts
auth middleware is misconfigured.web middleware (handles sessions):
Route::middleware(['web', 'auth'])->group(...);
Log Channel Misconfiguration
log_viewer channel is set as default in config/app.php and that the LogViewerChannel is properly registered.Custom Log Fields
Log model to add metadata (e.g., user_id, ip_address):
public function setIpAttribute($value)
{
$this->attributes['ip_address'] = $value;
}
Log Anonymization
use SDTech\LogViewer\LogChannels\LogViewerChannel;
class CustomLogViewerChannel extends LogViewerChannel
{
protected function prepareLog(array $log)
{
$log['message'] = str_replace('password=.*&', 'password=***&', $log['message']);
return parent::prepareLog($log);
}
}
API Access
Route::middleware('api')->get('/api/logs', [LogViewerController::class, 'apiIndex']);
Update the controller to return JSON:
public function apiIndex(Request $request)
{
return \SDTech\LogViewer\Models\Log::query()
->filter($request)
->paginate(50)
->toJson();
}
Testing
use SDTech\LogViewer\Models\Log;
public function test_log_storage()
{
Log::factory()->create(['message' => 'Test log']);
$this->assertDatabaseHas('logs', ['message' => 'Test log']);
}
Log::flush() to clear test logs between tests.How can I help you explore Laravel packages today?