Last active
August 4, 2024 12:54
-
-
Save aytacmalkoc/2ec63805d6311ddd2228e7d6cd42104b to your computer and use it in GitHub Desktop.
Revisions
-
aytacmalkoc revised this gist
Aug 4, 2024 . 1 changed file with 22 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -30,10 +30,29 @@ protected static function logAction($action, $model, $isDeleted = false): void $userId = auth()->id() ?? 'guest'; $className = get_class($model); $modelId = $model->getKey(); $logLevel = self::logType($action); if ($action === 'updated') { $original = $model->getOriginal(); $changes = $model->getDirty(); Log::channel('hub')->$logLevel("User {$userId} has {$action} a {$className} with ID {$modelId}", [ 'original' => $original, 'changes' => $changes ]); } elseif ($isDeleted) { $changes = $model->getAttributes(); Log::channel('hub')->$logLevel("User {$userId} has {$action} a {$className} with ID {$modelId}", [ 'attributes' => $changes ]); } else { $changes = $model->getDirty(); Log::channel('hub')->$logLevel("User {$userId} has {$action} a {$className} with ID {$modelId}", [ 'changes' => $changes ]); } } protected static function logType(string $action): string @@ -45,4 +64,4 @@ protected static function logType(string $action): string default => 'debug', }; } } -
aytacmalkoc created this gist
Aug 4, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ <?php namespace App\Traits; use Illuminate\Support\Facades\Log; trait HasCrudLogs { public static function bootHasCrudLogs(): void { static::created(function ($model) { self::logAction('created', $model); }); static::updated(function ($model) { self::logAction('updated', $model); }); static::deleted(function ($model) { self::logAction('deleted', $model, true); }); static::replicating(function ($model) { self::logAction('replicating', $model); }); } protected static function logAction($action, $model, $isDeleted = false): void { $userId = auth()->id() ?? 'guest'; $className = get_class($model); $modelId = $model->getKey(); $changes = $isDeleted ? $model->getAttributes() : $model->getDirty(); $logLevel = self::logType($action); Log::channel('hub')->$logLevel("User {$userId} is {$action} a {$className} with ID {$modelId}", ['changes' => $changes]); } protected static function logType(string $action): string { return match ($action) { 'creating', 'created' => 'info', 'updating', 'updated' => 'notice', 'deleting', 'deleted' => 'warning', default => 'debug', }; } }