-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1551 from AndyTWF/plugin-logging
feat: endpoint to capture logs from plugin
- Loading branch information
Showing
19 changed files
with
433 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources; | ||
|
||
use App\Filament\Resources\PluginLogResource\Pages; | ||
use App\Models\Plugin\PluginLog; | ||
use Filament\Forms\Components\Textarea; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Resources\Resource; | ||
use Filament\Resources\Table; | ||
use Filament\Resources\Form; | ||
use Filament\Tables\Columns\TextColumn; | ||
|
||
class PluginLogResource extends Resource | ||
{ | ||
use TranslatesStrings; | ||
|
||
protected static ?string $model = PluginLog::class; | ||
|
||
protected static ?string $navigationIcon = 'heroicon-o-rss'; | ||
protected static ?string $navigationGroup = 'Administration'; | ||
|
||
public static function form(Form $form): Form | ||
{ | ||
return $form | ||
->schema([ | ||
TextInput::make('id') | ||
->label(self::translateFormPath('id.label')) | ||
->required(), | ||
TextInput::make('type') | ||
->label(self::translateFormPath('type.label')) | ||
->required(), | ||
TextInput::make('message') | ||
->label(self::translateFormPath('message.label')) | ||
->required(), | ||
Textarea::make('metadata') | ||
->columnSpan('full') | ||
->formatStateUsing(fn (array|null $state) => json_encode($state, JSON_PRETTY_PRINT)) | ||
->label(self::translateFormPath('metadata.label')) | ||
->required(), | ||
]); | ||
} | ||
|
||
public static function table(Table $table): Table | ||
{ | ||
return $table | ||
->columns([ | ||
TextColumn::make('id') | ||
->label(self::translateTablePath('columns.id')), | ||
TextColumn::make('type') | ||
->label(self::translateTablePath('columns.type')), | ||
TextColumn::make('created_at') | ||
->label(self::translateTablePath('columns.created_at')), | ||
]); | ||
} | ||
|
||
public static function getPages(): array | ||
{ | ||
return [ | ||
'index' => Pages\ListPluginLogs::route('/'), | ||
'view' => Pages\ViewPluginLog::route('/{record}'), | ||
]; | ||
} | ||
|
||
protected static function translationPathRoot(): string | ||
{ | ||
return 'plugin'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
app/Filament/Resources/PluginLogResource/Pages/ListPluginLogs.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\PluginLogResource\Pages; | ||
|
||
use App\Filament\Resources\Pages\LimitsTableRecordListingOptions; | ||
use App\Filament\Resources\PluginLogResource; | ||
use Filament\Resources\Pages\ListRecords; | ||
|
||
class ListPluginLogs extends ListRecords | ||
{ | ||
use LimitsTableRecordListingOptions; | ||
|
||
protected static string $resource = PluginLogResource::class; | ||
} |
11 changes: 11 additions & 0 deletions
11
app/Filament/Resources/PluginLogResource/Pages/ViewPluginLog.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Filament\Resources\PluginLogResource\Pages; | ||
|
||
use App\Filament\Resources\PluginLogResource; | ||
use Filament\Resources\Pages\ViewRecord; | ||
|
||
class ViewPluginLog extends ViewRecord | ||
{ | ||
protected static string $resource = PluginLogResource::class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Plugin\PluginLog; | ||
use App\Http\Requests\Plugin\PluginLog as PluginLogRequest; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class PluginLogController extends BaseController | ||
{ | ||
public function createPluginLogEntry(PluginLogRequest $log): JsonResponse | ||
{ | ||
$validated = $log->validated(); | ||
if (isset($validated['metadata'])) { | ||
$validated['metadata'] = json_decode($validated['metadata']); | ||
} | ||
|
||
$pluginLog = PluginLog::create($validated); | ||
return response()->json(['id' => $pluginLog->id], 201); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Plugin; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class PluginLog extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'type' => 'required|string|max:255', | ||
'message' => 'required|string|max:255', | ||
'metadata' => 'nullable|json', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Models\Plugin; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class PluginLog extends Model | ||
{ | ||
use HasFactory; | ||
use HasUuids; | ||
|
||
const UPDATED_AT = null; | ||
|
||
protected $fillable = [ | ||
'type', | ||
'message', | ||
'metadata', | ||
]; | ||
|
||
protected $casts = [ | ||
'metadata' => 'array', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Database\Factories\Plugin; | ||
|
||
use App\Models\Plugin\PluginLog; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class PluginLogFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = PluginLog::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'type' => 'FATAL_EXCEPTION', | ||
'message' => $this->faker->sentence(), | ||
'metadata' => [ | ||
'foo' => $this->faker->sentence(), | ||
'bar' => $this->faker->sentence(), | ||
], | ||
]; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
database/migrations/2024_04_14_132135_create_plugin_logs_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('plugin_logs', function (Blueprint $table) { | ||
$table->uuid('id')->primary(); | ||
$table->string('type')->index()->comment('The type of log, for easy searching'); | ||
$table->text('message')->comment('The message of the log'); | ||
$table->json('metadata')->nullable()->comment('The context of the log'); | ||
$table->timestamp('created_at')->comment('The time the log was created'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('plugin_logs'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
use App\Models\Plugin\PluginLog; | ||
use Illuminate\Database\Seeder; | ||
|
||
class PluginLogTableSeeder extends Seeder | ||
{ | ||
public function run() | ||
{ | ||
PluginLog::factory()->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
return [ | ||
'id' => [ | ||
'label' => 'ID', | ||
], | ||
'type' => [ | ||
'label' => 'Type', | ||
], | ||
'message' => [ | ||
'label' => 'Message', | ||
], | ||
'metadata' => [ | ||
'label' => 'Metadata', | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
return [ | ||
'columns' => [ | ||
'id' => 'ID', | ||
'type' => 'Type', | ||
'created_at' => 'Timestamp', | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.