Skip to content

Commit

Permalink
fix that SquashedLogger not print Throwable class and message (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed May 21, 2024
1 parent 4f40c60 commit af96a3d
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class SquashedLogger {
private final Logger logger;
private final long interval;

private volatile AtomicLong lastLogTime;
private final AtomicLong lastLogTime;

private SquashedLogger(Logger logger, int intervalSeconds) {
this.logger = logger;
this.lastLogTime = new AtomicLong(0);
this.interval = Duration.ofSeconds(intervalSeconds).toNanos();
this.lastLogTime = new AtomicLong(System.nanoTime() - interval);
}

public static SquashedLogger getLogger(Logger target, int intervalSeconds) {
Expand All @@ -42,7 +42,7 @@ public static SquashedLogger getLogger(Logger target) {
private boolean shouldLogEx() {
long now = System.nanoTime();
long last = lastLogTime.get();
if (Math.abs(now - last) > interval) {
if (Math.abs(now - last) >= interval) {
return lastLogTime.compareAndSet(last, now);
} else {
return false;
Expand All @@ -61,14 +61,15 @@ public void error(CharSequence msg, Throwable e) {
sb.append(msg);
}
sb.append(' ');
while (e != null) {
int i = 0;
while (e != null && i++ < 20) {
sb.append(e);
e = e.getCause();
if (e != null) {
sb.append("\ncause by ");
}
}
logger.error(msg.toString());
logger.error(sb.toString());
}
}
}

0 comments on commit af96a3d

Please sign in to comment.