View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core;
18  
19  import java.io.Serializable;
20  
21  /**
22   * Lays out a {@linkplain LogEvent} in different formats. 
23   * The formats are: byte[], or an implementor of {@linkplain Serializable}, like {@code byte[]},
24   * String, and LogEvent.
25   * 
26   * @doubt There is still a need for a character-based layout for character based event sinks (databases, etc). Would introduce an
27   *        EventEncoder, EventRenderer or something similar for the logging event to byte encoding. (RG) A layout can be configured with a
28   *        Charset and then Strings can be converted to byte arrays. OTOH, it isn't possible to write byte arrays as character streams.
29   * @param <T>
30   *            The Object type that will be returned on the {@link #toSerializable(LogEvent)} call.
31   */
32  public interface Layout<T extends Serializable> {
33      /**
34       * Returns the format for the layout format.
35       * @return The footer.
36       * @doubt the concept of header and footer is not universal, should not be on the base interface.
37       * (RG) I agree with this.
38       */
39      byte[] getFooter();
40  
41      /**
42       * Returns the header for the layout format.
43       * @return The header.
44       * @doubt the concept of header and footer is not universal, should not be on the base interface.
45       * (RG) I agree with this.
46       */
47      byte[] getHeader();
48  
49      /**
50       * Formats the event suitable for display.
51       *
52       * @param event The Logging Event.
53       * @return The formatted event.
54       * @doubt Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the
55       * Appender can use the Layout. For example, it might concatenate information in front or behind the
56       * data and then write it all to the OutputStream in one call.
57       */
58      byte[] toByteArray(LogEvent event);
59  
60      /**
61       * Formats the event as an Object that can be serialized.
62       *
63       * @param event The Logging Event.
64       * @return The formatted event.
65       */
66      T toSerializable(LogEvent event);
67  
68  
69  }