123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace App\libs\helpers;
- use App\libs\cache\redis\Redis;
- use Illuminate\Support\Facades\Config;
- use Illuminate\Support\Facades\DB;
- use App\Auth;
- /**
- * 日志统计类
- *
- * @package lc\helpers
- */
- class LogHelper
- {
- /**
- * 记录 exception 日志
- *
- * @param $exception
- * @param $errCode
- * @return void
- */
- public static function exceptionLog($exception, $errCode)
- {
- $names = explode('@',request()->route()->getAction()['controller']);
- $data = [
- 'token' => request()->header('token', null),
- 'uid' => Auth::$userId ? Auth::$userId : Auth::$adminId,
- 'code' => $errCode,
- 'controller' => $names[0],
- 'func' => $names[1],
- 'method' => request()->method(),
- 'ip' => request()->ip(),
- 'params' => json_encode([
- 'message' => $exception->getMessage(),
- 'code' => $exception->getCode(),
- 'line' => $exception->getLine(),
- 'file' => $exception->getFile()
- ], JSON_UNESCAPED_UNICODE),
- 'day' => strtotime('today'),
- 'created_at' => time()
- ];
- Db::table('exception_logs')->insert($data);
- }
- /**
- * 记录第三方日志
- *
- * @param string $type 日志类型
- * @param string $funcName 调用方法
- * @param array $bodyParams 请求 body 参数
- * @param array $response 返回数据
- * @param array $headers 请求头
- * @param array $params 请求 query 参数
- */
- public static function thirdLog(string $type, string $funcName, array $bodyParams, array $response, array $headers = [], array $params = [])
- {
- $data = [
- 'uid' => Auth::$userId ? Auth::$userId : Auth::$adminId,
- 'type' => $type,
- 'func_name' => $funcName,
- 'headers' => json_encode($headers, JSON_UNESCAPED_UNICODE),
- 'params' => json_encode($params, JSON_UNESCAPED_UNICODE),
- 'body' => json_encode($bodyParams, JSON_UNESCAPED_UNICODE),
- 'response' => Helper::filterEmoji(json_encode($response, JSON_UNESCAPED_UNICODE)),
- 'created_at' => time(),
- ];
- Db::table('third_logs')->insert($data);
- }
- /**
- * 记录用户日志
- *
- * @param string $event 用户事件
- * @param string $remark 备注
- * @param array $moreInfo 数据信息
- */
- public static function userLog(string $event, string $remark, array $moreInfo)
- {
- $data = [
- 'uid' => Auth::$userId ? Auth::$userId : Auth::$adminId,
- 'event' => $event,
- 'remark' => $remark,
- 'more_info' => json_encode($moreInfo, JSON_UNESCAPED_UNICODE),
- 'created_at' => time()
- ];
- Db::table('user_logs')->insert($data);
- }
- /**
- * 数据记录
- *
- * @param $field
- * @param int $cnt
- */
- public static function record($field, $cnt = 1)
- {
- $hour = strtotime(date('Y-m-d H:00:00'));
- $res = Db::name('record')->where(['hour' => $hour])->inc($field, $cnt)->update();
- if (!$res) {
- Db::table('record')
- ->insert([
- 'hour' => $hour,
- $field => $cnt
- ]);
- }
- }
- }
|