ModelExtended.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App;
  3. use App\Services\LogService;
  4. use Illuminate\Database\Eloquent\Model;
  5. class ModelExtended extends Model
  6. {
  7. protected $isNotLogging=[
  8. Log::class
  9. ];
  10. private $isShouldLog=null;
  11. protected function log($input=''):bool{
  12. if($this->isShouldLog===null)
  13. $this->isShouldLog
  14. =array_search(get_class($this),$this->isNotLogging)===false;
  15. if(!$this->isShouldLog)return false;
  16. $traces=json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,5));
  17. preg_match('/\"function\"\:\".*?"function\"\:\"(.*?)\"\,\"class\"\:\".*?\"/', $traces, $result);
  18. $methodName = $result[1]??'';
  19. LogService::log(get_class($this),$methodName,
  20. '对象:'.$this->toJson()
  21. .'参数:'.json_encode([$input])
  22. .'调用堆栈:'.$traces
  23. );
  24. return $this->isShouldLog;
  25. }
  26. function save(array $options = [])
  27. {
  28. $this->log($options);
  29. return parent::save($options); // TODO: Change the autogenerated stub
  30. }
  31. function update(array $attributes = [], array $options = [])
  32. {
  33. $this->log([$attributes, $options]);
  34. return parent::update($attributes, $options); // TODO: Change the autogenerated stub
  35. }
  36. public function delete()
  37. {
  38. $this->log();
  39. return parent::delete(); // TODO: Change the autogenerated stub
  40. }
  41. }