LogHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\libs\helpers;
  3. use App\Auth;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * 日志统计类.
  7. */
  8. class LogHelper
  9. {
  10. /**
  11. * 记录 exception 日志.
  12. */
  13. public static function exceptionLog($exception, $errCode): void
  14. {
  15. var_dump($exception->getMessage());
  16. $names = explode('@', request()->route()->getAction()['controller']);
  17. $data = [
  18. 'token' => request()->header('token', null),
  19. 'uid' => Auth::$userId ? Auth::$userId : Auth::$adminId,
  20. 'code' => $errCode,
  21. 'controller' => $names[0],
  22. 'func' => $names[1],
  23. 'method' => request()->method(),
  24. 'ip' => request()->ip(),
  25. 'params' => json_encode([
  26. 'message' => $exception->getMessage(),
  27. 'code' => $exception->getCode(),
  28. 'line' => $exception->getLine(),
  29. 'file' => $exception->getFile(),
  30. ], JSON_UNESCAPED_UNICODE),
  31. 'day' => strtotime('today'),
  32. 'created_at' => time(),
  33. ];
  34. Db::table('exception_logs')->insert($data);
  35. }
  36. /**
  37. * 记录第三方日志.
  38. *
  39. * @param string $type 日志类型
  40. * @param string $funcName 调用方法
  41. * @param array $bodyParams 请求 body 参数
  42. * @param array $response 返回数据
  43. * @param array $headers 请求头
  44. * @param array $params 请求 query 参数
  45. */
  46. public static function thirdLog(string $type, string $funcName, array $bodyParams, array $response, array $headers = [], array $params = []): void
  47. {
  48. $data = [
  49. 'uid' => Auth::$userId ? Auth::$userId : Auth::$adminId,
  50. 'type' => $type,
  51. 'func_name' => $funcName,
  52. 'headers' => json_encode($headers, JSON_UNESCAPED_UNICODE),
  53. 'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
  54. 'body' => json_encode($bodyParams, JSON_UNESCAPED_UNICODE),
  55. 'response' => Helper::filterEmoji(json_encode($response, JSON_UNESCAPED_UNICODE)),
  56. 'created_at' => time(),
  57. ];
  58. Db::table('third_logs')->insert($data);
  59. }
  60. /**
  61. * 记录用户日志.
  62. *
  63. * @param string $event 用户事件
  64. * @param string $value 值
  65. * @param array $moreInfo 数据信息
  66. */
  67. public static function userLog(string $event, string $value = '', array $moreInfo = []): void
  68. {
  69. $data = [
  70. 'user_id' => Auth::$userId ? Auth::$userId : Auth::$adminId,
  71. 'event' => $event,
  72. 'value' => $value,
  73. 'more' => json_encode($moreInfo, JSON_UNESCAPED_UNICODE),
  74. 'created_at' => time(),
  75. 'day' => strtotime('today'),
  76. 'num' => '1',
  77. ];
  78. Db::table('logs')->insert($data);
  79. }
  80. /**
  81. * 数据记录.
  82. *
  83. * @param int $cnt
  84. */
  85. public static function record($field, $cnt = 1): void
  86. {
  87. $hour = strtotime(date('Y-m-d H:00:00'));
  88. $res = Db::name('record')->where(['hour' => $hour])->inc($field, $cnt)->update();
  89. if (!$res) {
  90. Db::table('record')
  91. ->insert([
  92. 'hour' => $hour,
  93. $field => $cnt,
  94. ])
  95. ;
  96. }
  97. }
  98. }