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  import org.apache.logging.log4j.core.layout.ByteBufferDestination;
23  import org.apache.logging.log4j.core.layout.Encoder;
24  
25  /**
26   * Lays out a {@linkplain LogEvent} in different formats.
27   *
28   * The formats are:
29   * <ul>
30   * <li>
31   * {@code byte[]}</li>
32   * <li>
33   * an implementer of {@linkplain Serializable}, like {@code byte[]}</li>
34   * <li>
35   * {@linkplain String}</li>
36   * <li>
37   * {@linkplain LogEvent}</li>
38   * </ul>
39   * <p>
40   * Since 2.6, Layouts can {@linkplain Encoder#encode(Object, ByteBufferDestination) encode} a {@code LogEvent} directly
41   * to a {@link ByteBufferDestination} without creating temporary intermediary objects.
42   * </p>
43   *
44   * @param <T>
45   *            The {@link Serializable} type returned by {@link #toSerializable(LogEvent)}
46   */
47  public interface Layout<T extends Serializable> extends Encoder<LogEvent> {
48  
49      /**
50       * Main {@linkplain org.apache.logging.log4j.core.config.plugins.Plugin#elementType() plugin element type} for
51       * Layout plugins.
52       *
53       * @since 2.1
54       */
55      String ELEMENT_TYPE = "layout";
56  
57      /**
58       * Returns the format for the layout format.
59       * @return The footer.
60       */
61      byte[] getFooter();
62  
63      /**
64       * Returns the header for the layout format.
65       * @return The header.
66       */
67      byte[] getHeader();
68  
69      /**
70       * Formats the event suitable for display.
71       *
72       * @param event The Logging Event.
73       * @return The formatted event.
74       */
75      byte[] toByteArray(LogEvent event);
76  
77      /**
78       * Formats the event as an Object that can be serialized.
79       *
80       * @param event The Logging Event.
81       * @return The formatted event.
82       */
83      T toSerializable(LogEvent event);
84  
85      /**
86       * Returns the content type output by this layout. The base class returns "text/plain".
87       *
88       * @return the content type.
89       */
90      String getContentType();
91  
92      /**
93       * Returns a description of the content format.
94       *
95       * @return a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content
96       * format descriptors are specified.
97       */
98      Map<String, String> getContentFormat();
99  }