View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.blackbox.net;
15  
16  import ch.qos.logback.core.boolex.EvaluationException;
17  import ch.qos.logback.core.boolex.EventEvaluator;
18  import ch.qos.logback.core.spi.ContextAwareBase;
19  
20  /**
21   * A simple EventEvaluator implementation that triggers email transmission after
22   * a given number of events occur, regardless of event level.
23   * 
24   * <p>
25   * By default, the limit is 1024.
26   */
27  public class CounterBasedEvaluator extends ContextAwareBase implements EventEvaluator<Object> {
28  
29      static int DEFAULT_LIMIT = 1024;
30      int limit = DEFAULT_LIMIT;
31      int counter = 0;
32      String name;
33      boolean started;
34  
35      public boolean evaluate(Object event) throws NullPointerException, EvaluationException {
36          counter++;
37  
38          if (counter == limit) {
39              counter = 0;
40              return true;
41          } else {
42              return false;
43          }
44      }
45  
46      public String getName() {
47          return name;
48      }
49  
50      public void setName(String name) {
51          this.name = name;
52      }
53  
54      public boolean isStarted() {
55          return started;
56      }
57  
58      public void start() {
59          started = true;
60      }
61  
62      public void stop() {
63          started = false;
64      }
65  
66      public int getLimit() {
67          return limit;
68      }
69  
70      public void setLimit(int limit) {
71          this.limit = limit;
72      }
73  
74  }