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     */
017    package org.apache.logging.log4j.core;
018    
019    import java.io.Serializable;
020    
021    /**
022     * Lays out a {@linkplain LogEvent} in different formats.
023     *
024     * The formats are:
025     * <ul>
026     * <li>
027     * {@code byte[]}</li>
028     * <li>
029     * an implementor of {@linkplain Serializable}, like {@code byte[]}</li>
030     * <li>
031     * {@linkplain String}</li>
032     * <li>
033     * {@linkplain LogEvent}</li>
034     * </ul>
035     *
036     * @param <T>
037     *            The {@link Serializable} type returned by {@link #toSerializable(LogEvent)}
038     *
039     * @doubt There is still a need for a character-based layout for character based event sinks (databases, etc). Would
040     * introduce an EventEncoder, EventRenderer or something similar for the logging event to byte encoding. (RG) A layout
041     * can be configured with a Charset and then Strings can be converted to byte arrays. OTOH, it isn't possible to write
042     * byte arrays as character streams.
043     */
044    public interface Layout<T extends Serializable> {
045        /**
046         * Returns the format for the layout format.
047         * @return The footer.
048         * @doubt the concept of header and footer is not universal, should not be on the base interface.
049         * (RG) I agree with this.
050         */
051        byte[] getFooter();
052    
053        /**
054         * Returns the header for the layout format.
055         * @return The header.
056         * @doubt the concept of header and footer is not universal, should not be on the base interface.
057         * (RG) I agree with this.
058         */
059        byte[] getHeader();
060    
061        /**
062         * Formats the event suitable for display.
063         *
064         * @param event The Logging Event.
065         * @return The formatted event.
066         * @doubt Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the
067         * Appender can use the Layout. For example, it might concatenate information in front or behind the
068         * data and then write it all to the OutputStream in one call.
069         */
070        byte[] toByteArray(LogEvent event);
071    
072        /**
073         * Formats the event as an Object that can be serialized.
074         *
075         * @param event The Logging Event.
076         * @return The formatted event.
077         */
078        T toSerializable(LogEvent event);
079    
080        /**
081         * Returns the content type output by this layout. The base class returns "text/plain".
082         *
083         * @return the content type.
084         */
085        String getContentType();
086    }