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       */
50      byte[] getFooter();
51  
52      /**
53       * Returns the header for the layout format.
54       * @return The header.
55       */
56      byte[] getHeader();
57  
58      /**
59       * Formats the event suitable for display.
60       *
61       * @param event The Logging Event.
62       * @return The formatted event.
63       * @doubt Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the
64       * Appender can use the Layout. For example, it might concatenate information in front or behind the
65       * data and then write it all to the OutputStream in one call.
66       */
67      byte[] toByteArray(LogEvent event);
68  
69      /**
70       * Formats the event as an Object that can be serialized.
71       *
72       * @param event The Logging Event.
73       * @return The formatted event.
74       */
75      T toSerializable(LogEvent event);
76  
77      /**
78       * Returns the content type output by this layout. The base class returns "text/plain".
79       *
80       * @return the content type.
81       */
82      String getContentType();
83  
84      /**
85       * Returns a description of the content format.
86       *
87       * @return a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content format descriptors are specified.
88       *
89       */
90      Map<String, String> getContentFormat();
91  }