Administrator
9 hours ago bf1b934566ab9aa365a239dd16c8486d9cd6e9e2
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.xcong.excoin.modules.gateApi;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
 
/**
 * Gate 策略日志环形缓冲区,支持 SSE 推送到前端。
 * 最多保留 500 条日志,新客户端连接时先推送历史再持续推新日志。
 */
@Slf4j
@Service
public class GateLogBuffer {
 
    private static final int MAX_ENTRIES = 500;
    private static final DateTimeFormatter TF = DateTimeFormatter.ofPattern("HH:mm:ss");
 
    private final List<String> entries = Collections.synchronizedList(new ArrayList<>(MAX_ENTRIES));
    private final List<SseEmitter> emitters = new CopyOnWriteArrayList<>();
 
    /**
     * 推送一条日志到所有 SSE 客户端。
     */
    public void push(String level, String message) {
        String line = String.format("[%s] %-5s %s", TF.format(LocalTime.now()), level, message);
 
        synchronized (entries) {
            if (entries.size() >= MAX_ENTRIES) {
                entries.remove(0);
            }
            entries.add(line);
        }
 
        // 推送给所有 SSE 客户端
        for (SseEmitter emitter : emitters) {
            try {
                emitter.send(SseEmitter.event().name("log").data(line));
            } catch (IOException e) {
                emitters.remove(emitter);
            }
        }
    }
 
    /** info 级别日志 */
    public void info(String message) { push("INFO", message); }
    /** warn 级别日志 */
    public void warn(String message) { push("WARN", message); }
    /** error 级别日志 */
    public void error(String message) { push("ERROR", message); }
 
    /**
     * 创建 SSE 连接,先推送历史再持续推送新日志。
     */
    public SseEmitter subscribe() {
        SseEmitter emitter = new SseEmitter(0L); // 无超时
        emitters.add(emitter);
 
        // 推送历史
        List<String> history;
        synchronized (entries) {
            history = new ArrayList<>(entries);
        }
        for (String line : history) {
            try {
                emitter.send(SseEmitter.event().name("log").data(line));
            } catch (IOException e) {
                emitters.remove(emitter);
                return emitter;
            }
        }
 
        emitter.onCompletion(() -> emitters.remove(emitter));
        emitter.onTimeout(() -> emitters.remove(emitter));
        emitter.onError(e -> emitters.remove(emitter));
 
        return emitter;
    }
}