Merge branch 'feature/REQ-2997' into 'master'

Feature/req 2997

See merge request universal/infrastructure/backend/axzo-log-plat!140
This commit is contained in:
陈文健 2024-12-23 03:33:23 +00:00
commit bedf4550b6
2 changed files with 25 additions and 17 deletions

View File

@ -1,5 +1,6 @@
package cn.axzo.log.platform.server.controller.web;
import cn.axzo.framework.auth.annotation.PreBuildContext;
import cn.axzo.log.platform.client.model.req.LogAddReq;
import cn.axzo.log.platform.client.model.req.LogBatchAddReq;
import cn.axzo.log.platform.client.model.req.LogBatchDeleteReq;
@ -7,13 +8,10 @@ import cn.axzo.log.platform.client.model.req.LogFindReq;
import cn.axzo.log.platform.client.model.resp.LogResp;
import cn.axzo.log.platform.server.dto.FindLogDto;
import cn.axzo.log.platform.server.dto.FindLogResp;
import cn.axzo.log.platform.server.entity.LogEntity;
import cn.axzo.log.platform.server.service.LogService;
import cn.azxo.framework.common.model.CommonResponse;
import com.mongodb.BasicDBObject;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -30,6 +28,7 @@ import java.util.List;
* @version 1.0
* @date 2024/9/11 18:16
*/
@PreBuildContext
@RestController
@RequestMapping("/webApi/log")
@RequiredArgsConstructor

View File

@ -1,6 +1,8 @@
package cn.axzo.log.platform.server.service.impl;
import cn.axzo.basics.common.util.NumberUtil;
import cn.axzo.framework.auth.domain.ContextInfo;
import cn.axzo.framework.auth.domain.ContextInfoHolder;
import cn.axzo.framework.domain.ServiceException;
import cn.axzo.log.platform.client.model.Condition;
import cn.axzo.log.platform.client.model.req.LogAddReq;
@ -430,17 +432,17 @@ public class LogServiceImpl implements LogService {
* @return true合法false非法
*/
public static Boolean isValidJsonObject(String str) {
if (JSONUtil.isTypeJSON(str)) {
if (JSONUtil.isTypeJSONObject(str)) {
try {
JSONUtil.parseObj(str);
JSONObject.parseObject(str);
return true;
} catch (Exception e1) {
try {
JSONUtil.parseArray(str);
return true;
} catch (Exception e2) {
return false;
}
// try {
// JSONUtil.parseArray(str);
// return true;
// } catch (Exception e2) {
// return false;
// }
}
}
return false;
@ -449,16 +451,23 @@ public class LogServiceImpl implements LogService {
@NotNull
private LogEntity buildToLogEntity(LogAddReq req) {
String message = req.getMessage();
if (JSONUtil.isTypeJSONArray(req.getMessage())) {
message = "{" + MESSAGE_FIELD_ARRAY_DEFAULT_KEY + ":" + req.getMessage() + "}";
}
// 判断req.message字段是否是一个合法的json字符串
// if (JSONUtil.isTypeJSONArray(req.getMessage())) {
// message = "{" + MESSAGE_FIELD_ARRAY_DEFAULT_KEY + ":" + req.getMessage() + "}";
// }
// 判断req.message字段是否是一个合法的jsonObject字符串
if (Boolean.TRUE.equals(!isValidJsonObject(message))) {
throw new ServiceException("message is not a valid json string, message: " + message);
throw new ServiceException("message is not a valid json object string, message: " + message);
}
LogEntity logEntity = BeanUtil.copyProperties(req, LogEntity.class, "message");
logEntity.setTimestamp(System.currentTimeMillis());
logEntity.setMessage(Document.parse(message));
Document msgDoc = Document.parse(message);
// 将用户上下文信息添加到message字段中
ContextInfo contextInfo = ContextInfoHolder.get();
if (Objects.nonNull(contextInfo)) {
msgDoc.append("userContextInfo", Document.parse(JSONObject.toJSONString(contextInfo)));
}
logEntity.setMessage(msgDoc);
return logEntity;
}
}