LogHelper.php 3.3 KB

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