ApiLogJob.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Queue\SerializesModels;
  5. use Illuminate\Queue\InteractsWithQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Schema;
  11. class ApiLogJob implements ShouldQueue
  12. {
  13. use Dispatchable;
  14. use InteractsWithQueue;
  15. use Queueable;
  16. use SerializesModels;
  17. private $data;
  18. /**
  19. * Create a new job instance.
  20. *
  21. * @param array $data
  22. */
  23. public function __construct(array $data)
  24. {
  25. $this->data = $data;
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. if (app()->environment(['production', 'release'])) {
  35. try {
  36. $tablename = "kdgx_fpdx_apilog" . date("Ymd");
  37. $exists = Cache::get("fpdx:apilog:table:exists", false);
  38. if (!$exists) {
  39. if (!Schema::connection('mysql_datalog')->hasTable($tablename)) {
  40. Schema::connection('mysql_datalog')->create($tablename, function ($table) {
  41. $table->increments('id');
  42. $table->string('uri', 128)->comment('uri');
  43. $table->string('method', 8)->comment('请求方法');
  44. $table->string('controller', 128)->comment('控制器');
  45. $table->string('host', 128)->comment('请求域名');
  46. $table->integer('uid')->comment('用户');
  47. $table->unsignedInteger('ip')->comment('ip');
  48. $table->double('begin_at', 16, 6)->comment('请求开始时间');
  49. $table->double('end_at', 16, 6)->comment('请求结束时间');
  50. $table->longText('request')->comment('请求包');
  51. $table->longText('response')->comment('响应包');
  52. $table->smallInteger('http_status')->comment('响应码');
  53. $table->longText('func_line')->comment('函数调用栈');
  54. $table->longText('sql_line')->comment('SQL调用栈');
  55. $table->index('uri', 'idx_uri');
  56. $table->index('uid', 'idx_uid');
  57. $table->index('ip', 'idx_ip');
  58. $table->index('begin_at', 'idx_begin');
  59. });
  60. DB::connection('mysql_datalog')->statement("alter table {$tablename} comment 'API调用日志表'");
  61. } else {
  62. Cache::put("fpdx:apilog:table:exists", true, (mktime(0, 0, 0) + 86400 - time()) / 60);
  63. }
  64. }
  65. DB::connection('mysql_datalog')->table("kdgx_fpdx_apilog" . date("Ymd"))->insert([
  66. 'uri' => $this->data['uri'],
  67. 'method' => $this->data['method'],
  68. 'controller' => $this->data['controller'],
  69. 'uid' => $this->data['uid'],
  70. 'request' => $this->data["request"],
  71. 'response' => $this->data["response"],
  72. 'http_status' => $this->data['http_status'],
  73. 'func_line' => $this->data["func_line"],
  74. 'sql_line' => $this->data["sql_line"],
  75. 'ip' => $this->data['ip'],
  76. 'begin_at' => $this->data['begin_at'],
  77. 'end_at' => $this->data['end_at'],
  78. 'host' => $this->data['host']
  79. ]);
  80. } catch (\Exception $exception) {
  81. dump($exception);
  82. }
  83. }
  84. }
  85. }