Bu tam sorunu log4j kullanarak çözen bir Java sınıfı oluşturdum. Bir iletiyi günlüğe istediğinizde, sadece böyle bir şey yapmak:
LogConsolidated.log(logger, Level.WARN, 5000, "File: " + f + " not found.", e);
yerine:
logger.warn("File: " + f + " not found.", e);
o 1 defa maksimum şimdiye 5 saniye log yapar
ve baskılar kaç kez giriş yapmış olmalıydı (örneğin | x53 |). Açıkçası, çok fazla parametreye sahip olmamanız veya log.warn veya bir şey yaparak seviyeyi çekebilmek için bunu yapabilirsiniz, fakat bu benim kullanım durumum için işe yarıyor.
import java.util.HashMap;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class LogConsolidated {
private static HashMap<String, TimeAndCount> lastLoggedTime = new HashMap<>();
/**
* Logs given <code>message</code> to given <code>logger</code> as long as:
* <ul>
* <li>A message (from same class and line number) has not already been logged within the past <code>timeBetweenLogs</code>.</li>
* <li>The given <code>level</code> is active for given <code>logger</code>.</li>
* </ul>
* Note: If messages are skipped, they are counted. When <code>timeBetweenLogs</code> has passed, and a repeat message is logged,
* the count will be displayed.
* @param logger Where to log.
* @param level Level to log.
* @param timeBetweenLogs Milliseconds to wait between similar log messages.
* @param message The actual message to log.
* @param t Can be null. Will log stack trace if not null.
*/
public static void log(Logger logger, Level level, long timeBetweenLogs, String message, Throwable t) {
if (logger.isEnabledFor(level)) {
String uniqueIdentifier = getFileAndLine();
TimeAndCount lastTimeAndCount = lastLoggedTime.get(uniqueIdentifier);
if (lastTimeAndCount != null) {
synchronized (lastTimeAndCount) {
long now = System.currentTimeMillis();
if (now - lastTimeAndCount.time < timeBetweenLogs) {
lastTimeAndCount.count++;
return;
} else {
log(logger, level, "|x" + lastTimeAndCount.count + "| " + message, t);
}
}
} else {
log(logger, level, message, t);
}
lastLoggedTime.put(uniqueIdentifier, new TimeAndCount());
}
}
private static String getFileAndLine() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
boolean enteredLogConsolidated = false;
for (StackTraceElement ste : stackTrace) {
if (ste.getClassName().equals(LogConsolidated.class.getName())) {
enteredLogConsolidated = true;
} else if (enteredLogConsolidated) {
// We have now file/line before entering LogConsolidated.
return ste.getFileName() + ":" + ste.getLineNumber();
}
}
return "?";
}
private static void log(Logger logger, Level level, String message, Throwable t) {
if (t == null) {
logger.log(level, message);
} else {
logger.log(level, message, t);
}
}
private static class TimeAndCount {
long time;
int count;
TimeAndCount() {
this.time = System.currentTimeMillis();
this.count = 0;
}
}
}
Kontrol bu bağlantıyı http://stackoverflow.com/questions/8359839/how-to-log-repeated-warnings-only-once –
@SajanChandran: Ben "Kendi rulo" düşündüm ama Bu zaten yeterli bir sorun olduğunu umuyordu, zaten standart çözüm/en iyi uygulama vardı. Bunu kodluyorsam, büyük olasılıkla bir log4j sınıfını genişleteceğim, böylece kodlama yerine bir yapılandırma görevi olacaktır. –
Bu iyi bir ilk adım olabilir: http://logging.apache.org/log4j/2.x/manual/filters.html#BurstFilter/ - Belki de kendi kodunuzu buna benzer bir kod yazabilirsiniz. answer: https://stackoverflow.com/a/37619797/1520422 –