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  import java.util.Map;
21  
22  /**
23   * Lays out a {@linkplain LogEvent} in different formats.
24   *
25   * The formats are:
26   * <ul>
27   * <li>
28   * {@code byte[]}</li>
29   * <li>
30   * an implementer of {@linkplain Serializable}, like {@code byte[]}</li>
31   * <li>
32   * {@linkplain String}</li>
33   * <li>
34   * {@linkplain LogEvent}</li>
35   * </ul>
36   *
37   * @param <T>
38   *            The {@link Serializable} type returned by {@link #toSerializable(LogEvent)}
39   *
40   * @doubt There is still a need for a character-based layout for character based event sinks (databases, etc). Would
41   * introduce an EventEncoder, EventRenderer or something similar for the logging event to byte encoding. (RG) A layout
42   * can be configured with a Charset and then Strings can be converted to byte arrays. OTOH, it isn't possible to write
43   * byte arrays as character streams.
44   */
45  public interface Layout<T extends Serializable> {
46      /**
47       * Returns the format for the layout format.
48       * @return The footer.
49       * @doubt the concept of header and footer is not universal, should not be on the base interface.
50       * (RG) I agree with this.
51       */
52      byte[] getFooter();
53  
54      /**
55       * Returns the header for the layout format.
56       * @return The header.
57       * @doubt the concept of header and footer is not universal, should not be on the base interface.
58       * (RG) I agree with this.
59       */
60      byte[] getHeader();
61  
62      /**
63       * Formats the event suitable for display.
64       *
65       * @param event The Logging Event.
66       * @return The formatted event.
67       * @doubt Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the
68       * Appender can use the Layout. For example, it might concatenate information in front or behind the
69       * data and then write it all to the OutputStream in one call.
70       */
71      byte[] toByteArray(LogEvent event);
72  
73      /**
74       * Formats the event as an Object that can be serialized.
75       *
76       * @param event The Logging Event.
77       * @return The formatted event.
78       */
79      T toSerializable(LogEvent event);
80  
81      /**
82       * Returns the content type output by this layout. The base class returns "text/plain".
83       *
84       * @return the content type.
85       */
86      String getContentType();
87  
88      /**
89       * Returns a description of the content format.
90       *
91       * @return a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content format descriptors are specified. 
92       * 
93       */
94      Map<String, String> getContentFormat();
95  }