修改--增加log相关

This commit is contained in:
tjxt 2024-07-30 11:26:49 +08:00
parent c4a8463e58
commit d190f46ff2
3 changed files with 15 additions and 4 deletions

View File

@ -3,10 +3,10 @@ package com.bonus.ai.controller;
import com.bonus.ai.service.IOcrRecogService;
import com.bonus.ai.utils.FileTypeUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import static com.bonus.common.core.web.domain.AjaxResult.error;
/**
@ -16,6 +16,7 @@ import static com.bonus.common.core.web.domain.AjaxResult.error;
*/
@RestController
@RequestMapping("/ocr")
@Slf4j
public class OcrRecogController
{
@Autowired
@ -40,6 +41,7 @@ public class OcrRecogController
}
} catch (Exception e) {
e.printStackTrace();
log.error("数据上传失败:{}",e);
return error("数据上传失败!!!");
}

View File

@ -20,6 +20,8 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.file.*;
import java.time.LocalDateTime;
@ -59,8 +61,10 @@ public class OcrRecogServiceImpl implements IOcrRecogService {
try {
getfilePath = convertFilesToBase64(files);
idCardVo = processPythonResponse(getfilePath, recogurl);
log.info("获得data:{}",idCardVo);
ajax.put("data",idCardVo);
} catch (Exception e) {
log.error("调用大模型失败:{}",e);
ajax.put("601", "调用大模型服务出错");
e.printStackTrace();
}
@ -73,7 +77,6 @@ public class OcrRecogServiceImpl implements IOcrRecogService {
String currentDate = System.currentTimeMillis()+"";
String frontFileName = String.format(FRONT_FILE_NAME_TEMPLATE, idNumber, currentDate);
String backFileName = String.format(BACK_FILE_NAME_TEMPLATE, idNumber, currentDate);
try {
byte[] frontImageBytes = Base64.getDecoder().decode(getfilePath[0]);
byte[] backImageBytes = Base64.getDecoder().decode(getfilePath[1]);
@ -82,12 +85,14 @@ public class OcrRecogServiceImpl implements IOcrRecogService {
Files.write(frontFilePath, frontImageBytes);
Files.write(backFilePath, backImageBytes);
} catch (IOException e) {
log.error("获取身份证信息出错:{}",e);
ajax.put("602", "获取身份证信息出错");
e.printStackTrace();
}
saveRecognitionResult(idCardVo, ifComplete, frontFileName, backFileName,startTime);
}
log.info("服务层返回给控制层的数据:{}",ajax);
return ajax;
}
@ -103,11 +108,13 @@ public class OcrRecogServiceImpl implements IOcrRecogService {
e.printStackTrace();
}
}
log.info("base64格式的图片:{}",base64Strings);
return base64Strings;
}
private IdCardVo processPythonResponse(String[] strings, String recogurl) throws Exception {
if (strings.length != 2) {
log.info("Python响应数据不完整。");
throw new IllegalArgumentException("Python响应数据不完整。");
}
ImageUpload imageUpload = new ImageUpload();
@ -116,6 +123,7 @@ public class OcrRecogServiceImpl implements IOcrRecogService {
imageUpload.setType("1");
ResponseEntity<String> response = restTemplate.postForEntity(recogurl, imageUpload, String.class);
if (response.getStatusCodeValue() != 200) {
log.info("调用服务失败,状态码:{}",response.getStatusCodeValue());
throw new RestClientException("调用服务失败,状态码:" + response.getStatusCodeValue());
}
String json = response.getBody();
@ -125,6 +133,7 @@ public class OcrRecogServiceImpl implements IOcrRecogService {
json = StringEscapeUtils.unescapeJava(json);
Gson gson = new Gson();
IdCardVo idCardVo = gson.fromJson(json, IdCardVo.class);//此方法 如果里面为空 错误 解决
log.info("获取python服务得到的数据{}",idCardVo);
return idCardVo;
}
private void saveRecognitionResult(IdCardVo idCardVo, char ifComplete, String frontFileName, String backFileName,long startTime) {
@ -152,7 +161,7 @@ public class OcrRecogServiceImpl implements IOcrRecogService {
.recognizeTime(LocalDateTime.now())
.responseLong(responseTime)
.invokeIp(IpUtils.getIpAddr(ServletUtils.getRequest()))
// .updateBy(loginUser.getUserid()) // 假设getUserid()已返回正确用户ID
// .updateBy(loginUser.getUserid()) // 假设getUserid()已返回正确用户ID
.updateTime(LocalDateTime.now())
.build();
aiIdcardrecognizeMapper.insert(aiIdcardrecognizeResult);

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<property name="log.path" value="logs/bonus-file" />
<property name="log.path" value="logs/bonus-ai" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />