001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core;
018
019import java.io.Serializable;
020import java.util.Map;
021
022/**
023 * Lays out a {@linkplain LogEvent} in different formats.
024 *
025 * The formats are:
026 * <ul>
027 * <li>
028 * {@code byte[]}</li>
029 * <li>
030 * an implementer of {@linkplain Serializable}, like {@code byte[]}</li>
031 * <li>
032 * {@linkplain String}</li>
033 * <li>
034 * {@linkplain LogEvent}</li>
035 * </ul>
036 *
037 * @param <T>
038 *            The {@link Serializable} type returned by {@link #toSerializable(LogEvent)}
039 *
040 * @doubt There is still a need for a character-based layout for character based event sinks (databases, etc). Would
041 * introduce an EventEncoder, EventRenderer or something similar for the logging event to byte encoding. (RG) A layout
042 * can be configured with a Charset and then Strings can be converted to byte arrays. OTOH, it isn't possible to write
043 * byte arrays as character streams.
044 */
045public interface Layout<T extends Serializable> {
046    /**
047     * Returns the format for the layout format.
048     * @return The footer.
049     */
050    byte[] getFooter();
051
052    /**
053     * Returns the header for the layout format.
054     * @return The header.
055     */
056    byte[] getHeader();
057
058    /**
059     * Formats the event suitable for display.
060     *
061     * @param event The Logging Event.
062     * @return The formatted event.
063     * @doubt Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the
064     * Appender can use the Layout. For example, it might concatenate information in front or behind the
065     * data and then write it all to the OutputStream in one call.
066     */
067    byte[] toByteArray(LogEvent event);
068
069    // TODO: it would be nice to provide ByteBuffers alongside the byte[]s
070
071    /**
072     * Formats the event as an Object that can be serialized.
073     *
074     * @param event The Logging Event.
075     * @return The formatted event.
076     */
077    T toSerializable(LogEvent event);
078
079    /**
080     * Returns the content type output by this layout. The base class returns "text/plain".
081     *
082     * @return the content type.
083     */
084    String getContentType();
085
086    /**
087     * Returns a description of the content format.
088     *
089     * @return a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content format descriptors are specified.
090     *
091     */
092    Map<String, String> getContentFormat();
093}