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  package ch.qos.logback.core.encoder;
15  
16  import ch.qos.logback.core.spi.ContextAware;
17  import ch.qos.logback.core.spi.LifeCycle;
18  
19  /**
20   * Encoders are responsible for transform an incoming event into a byte array
21   * 
22   * @author Ceki Gülcü
23   * @author Joern Huxhorn
24   * @author Maarten Bosteels
25   * 
26   * @param <E> event type
27   * @since 0.9.19
28   */
29  public interface Encoder<E> extends ContextAware, LifeCycle {
30  
31      /**
32       * Get header bytes. This method is typically called upon opening of an output
33       * stream.
34       * 
35       * @return header bytes. Null values are allowed.
36       */
37      byte[] headerBytes();
38  
39      /**
40       * Encode an event as bytes.
41       * 
42       * @param event the event to encode
43       * @return the encoded bytes. Null values are allowed.
44       */
45      byte[] encode(E event);
46  
47      /**
48       * Get footer bytes. This method is typically called prior to the closing of the
49       * stream where events are written.
50       * 
51       * @return footer bytes. Null values are allowed.
52       */
53      byte[] footerBytes();
54  
55      /**
56       * Returns true if the encoder is stateful, false otherwise.
57       *
58       * <p>A stateful encoder keeps state between invocation of {@link #encode(E event)}
59       * method.</p>
60       *
61       * <p>A stateful encoder may produce different output for the same input event
62       * depending on its internal state. A stateless encoder will always produce
63       * the same output for the same input event.</p>
64       *
65       * @return true if the encoder is stateful, false otherwise.
66       * @since 1.6.4
67       */
68      default boolean isStateful() {
69          return false;
70      }
71  }