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 * TODO 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    /**
048     * Main plugin element type for Layout plugins.
049     *
050     * @since 2.1
051     */
052    String ELEMENT_TYPE = "layout";
053
054    /**
055     * Returns the format for the layout format.
056     * @return The footer.
057     */
058    byte[] getFooter();
059
060    /**
061     * Returns the header for the layout format.
062     * @return The header.
063     */
064    byte[] getHeader();
065
066    /**
067     * Formats the event suitable for display.
068     *
069     * @param event The Logging Event.
070     * @return The formatted event.
071     * TODO Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the
072     * Appender can use the Layout. For example, it might concatenate information in front or behind the
073     * data and then write it all to the OutputStream in one call.
074     */
075    byte[] toByteArray(LogEvent event);
076
077    // TODO: it would be nice to provide ByteBuffers alongside the byte[]s
078
079    /**
080     * Formats the event as an Object that can be serialized.
081     *
082     * @param event The Logging Event.
083     * @return The formatted event.
084     */
085    T toSerializable(LogEvent event);
086
087    /**
088     * Returns the content type output by this layout. The base class returns "text/plain".
089     *
090     * @return the content type.
091     */
092    String getContentType();
093
094    /**
095     * Returns a description of the content format.
096     *
097     * @return a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content
098     * format descriptors are specified.
099     */
100    Map<String, String> getContentFormat();
101}