LogHelper.php 3.4 KB

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