001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.turbo; 015 016import org.slf4j.Marker; 017 018import ch.qos.logback.classic.Level; 019import ch.qos.logback.classic.Logger; 020import ch.qos.logback.core.spi.FilterReply; 021 022/** 023 * 024 * See {@link http://logback.qos.ch/manual/filters.html#DuplicateMessageFilter} 025 * for details. 026 * 027 * @author Ceki Gulcu 028 * 029 */ 030public class DuplicateMessageFilter extends TurboFilter { 031 032 /** 033 * The default cache size. 034 */ 035 public static final int DEFAULT_CACHE_SIZE = 100; 036 /** 037 * The default number of allows repetitions. 038 */ 039 public static final int DEFAULT_ALLOWED_REPETITIONS = 5; 040 041 public int allowedRepetitions = DEFAULT_ALLOWED_REPETITIONS; 042 public int cacheSize = DEFAULT_CACHE_SIZE; 043 044 private LRUMessageCache msgCache; 045 046 @Override 047 public void start() { 048 msgCache = new LRUMessageCache(cacheSize); 049 super.start(); 050 } 051 052 @Override 053 public void stop() { 054 msgCache.clear(); 055 msgCache = null; 056 super.stop(); 057 } 058 059 @Override 060 public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) { 061 int count = msgCache.getMessageCountAndThenIncrement(format); 062 if (count <= allowedRepetitions) { 063 return FilterReply.NEUTRAL; 064 } else { 065 return FilterReply.DENY; 066 } 067 } 068 069 public int getAllowedRepetitions() { 070 return allowedRepetitions; 071 } 072 073 /** 074 * The allowed number of repetitions before 075 * 076 * @param allowedRepetitions 077 */ 078 public void setAllowedRepetitions(int allowedRepetitions) { 079 this.allowedRepetitions = allowedRepetitions; 080 } 081 082 public int getCacheSize() { 083 return cacheSize; 084 } 085 086 public void setCacheSize(int cacheSize) { 087 this.cacheSize = cacheSize; 088 } 089 090}