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