View Javadoc

1   package org.lsst.ccs.utilities.logging;
2   
3   import java.util.LinkedHashMap;
4   import java.util.Map;
5   import java.util.logging.LogRecord;
6   
7   /**
8    * A utility to plug into Handlers that want to detect duplicate logs.
9    * it keeps a list of the 32 last logs and return false to the <TT>isLoggable</TT> method
10   * if a <TT>LogRecord</TT> with the same sequence number has been processed.
11   * @author bamade
12   */
13  // Date: 29/05/13
14  
15  public class IsLoggableDelegate {
16      private static final int MAX_ENTRIES = 32;
17      private LinkedHashMap<Long, String> mapRecords =
18              // the size of the Map is the nearest prime number more than MAX_ENTRIES
19              new LinkedHashMap<Long, String>(37){
20                  @Override
21                  protected boolean removeEldestEntry(Map.Entry eldest) {
22                      return size() > MAX_ENTRIES;
23                  }
24              } ;
25      // if the logRecord is already in the map the put does not return null
26      public boolean isLoggable(LogRecord logRecord) {
27          String logger = logRecord.getLoggerName();
28          long seqNumber = logRecord.getSequenceNumber() ;
29          return null == mapRecords.put(seqNumber,logger) ;
30      }
31  }