Junjie
2 天以前 63b01db83d9aad8a15276b4236a9a22e4aeef065
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.zy.ai.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.ai.entity.LlmCallLog;
import com.zy.ai.mapper.LlmCallLogMapper;
import com.zy.ai.service.LlmCallLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
@Service("llmCallLogService")
@Slf4j
public class LlmCallLogServiceImpl extends ServiceImpl<LlmCallLogMapper, LlmCallLog> implements LlmCallLogService {
 
    private volatile boolean disabled = false;
    private volatile long lastRetryTime = 0;
    private static final long RETRY_INTERVAL_MS = 60_000; // 1 分钟后重试
 
    @Override
    public void saveIgnoreError(LlmCallLog logItem) {
        if (logItem == null) {
            return;
        }
        if (disabled) {
            // 定期重试,防止表后来创建了但 disabled 一直为 true
            long now = System.currentTimeMillis();
            if (now - lastRetryTime < RETRY_INTERVAL_MS) {
                return;
            }
            lastRetryTime = now;
            log.info("LLM调用日志之前已禁用,尝试重新写入...");
        }
        try {
            save(logItem);
            if (disabled) {
                disabled = false;
                log.info("LLM调用日志写入成功,已恢复日志记录");
            }
        } catch (Exception e) {
            String msg = e.getMessage() == null ? "" : e.getMessage();
            if (msg.contains("doesn't exist") || msg.contains("不存在")) {
                disabled = true;
                lastRetryTime = System.currentTimeMillis();
                log.warn("LLM调用日志表不存在,日志记录已暂停,请先执行建表SQL");
                return;
            }
            log.warn("写入LLM调用日志失败: {}", msg);
        }
    }
}