View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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  
15  package ch.qos.logback.access.common.boolex;
16  
17  import ch.qos.logback.access.common.spi.IAccessEvent;
18  import ch.qos.logback.core.boolex.EvaluationException;
19  import ch.qos.logback.core.boolex.EventEvaluatorBase;
20  
21  /**
22   * A very simple {@link ch.qos.logback.core.boolex.EventEvaluator EventEvaluator}
23   * which checks whether the status of {@link IAccessEvent access event} is equal
24   * to a status code given as a parameter.
25   *
26   * @since 2.0.6
27   */
28  public class StatusCodeEventEvaluator extends EventEvaluatorBase<IAccessEvent> {
29  
30      final static int UNSET = -1;
31      int statusCode = UNSET;
32  
33      @Override
34      public void start() {
35          if(statusCode == UNSET) {
36              addWarn("No status code set");
37              return;
38          }
39          super.start();
40      }
41      @Override
42      public boolean evaluate(IAccessEvent iAccessEvent) throws NullPointerException, EvaluationException {
43          if (!isStarted()) {
44              throw new IllegalStateException("Evaluator [" + this + "] was called in stopped state");
45          }
46          boolean result = (iAccessEvent.getStatusCode() == statusCode);
47          return result;
48      }
49  
50      public int getStatusCode() {
51          return statusCode;
52      }
53  
54      public void setStatusCode(int statusCode) {
55          this.statusCode = statusCode;
56      }
57  }