• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jetcache-core/src/main/java/com/alicp/jetcache/support/SquashedLogger.java
1
/**
2
 * Created on 2022/7/6.
3
 */
4
package com.alicp.jetcache.support;
5

6
import org.slf4j.Logger;
7

8
import java.time.Duration;
9
import java.util.concurrent.ConcurrentHashMap;
10
import java.util.concurrent.atomic.AtomicLong;
11

12
/**
13
 * @author huangli
14
 */
15
public class SquashedLogger {
16
    private static final int DEFAULT_INTERVAL_SECONDS = 10;
17

18
    private static final ConcurrentHashMap<Logger, SquashedLogger> MAP = new ConcurrentHashMap<>();
×
19
    private final Logger logger;
20
    private final long interval;
21

22
    private volatile AtomicLong lastLogTime;
23

24
    private SquashedLogger(Logger logger, int intervalSeconds) {
×
25
        this.logger = logger;
×
26
        this.lastLogTime = new AtomicLong(0);
×
27
        this.interval = Duration.ofSeconds(intervalSeconds).toNanos();
×
28
    }
×
29

30
    public static SquashedLogger getLogger(Logger target, int intervalSeconds) {
31
        SquashedLogger result = MAP.get(target);
×
32
        if (result == null) {
×
33
            result = MAP.computeIfAbsent(target, k -> new SquashedLogger(k, intervalSeconds));
×
34
        }
35
        return result;
×
36
    }
37

38
    public static SquashedLogger getLogger(Logger target) {
39
        return getLogger(target, DEFAULT_INTERVAL_SECONDS);
×
40
    }
41

42
    private boolean shouldLogEx() {
43
        long now = System.nanoTime();
×
44
        long last = lastLogTime.get();
×
45
        if (Math.abs(now - last) > interval) {
×
46
            return lastLogTime.compareAndSet(last, now);
×
47
        } else {
48
            return false;
×
49
        }
50
    }
51

52
    public void error(CharSequence msg, Throwable e) {
53
        if (shouldLogEx()) {
×
54
            logger.error(msg.toString(), e);
×
55
        } else {
56
            StringBuilder sb;
57
            if (msg instanceof StringBuilder) {
×
58
                sb = (StringBuilder) msg;
×
59
            } else {
60
                sb = new StringBuilder(msg.length() + 256);
×
61
                sb.append(msg);
×
62
            }
63
            sb.append(' ');
×
64
            while (e != null) {
×
65
                sb.append(e);
×
66
                e = e.getCause();
×
67
                if (e != null) {
×
68
                    sb.append("\ncause by ");
×
69
                }
70
            }
71
            logger.error(msg.toString());
×
72
        }
73
    }
×
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc