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 import 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 */
045 public interface Layout<T extends Serializable> {
046 /**
047 * Returns the format for the layout format.
048 * @return The footer.
049 * @doubt the concept of header and footer is not universal, should not be on the base interface.
050 * (RG) I agree with this.
051 */
052 byte[] getFooter();
053
054 /**
055 * Returns the header for the layout format.
056 * @return The header.
057 * @doubt the concept of header and footer is not universal, should not be on the base interface.
058 * (RG) I agree with this.
059 */
060 byte[] getHeader();
061
062 /**
063 * Formats the event suitable for display.
064 *
065 * @param event The Logging Event.
066 * @return The formatted event.
067 * @doubt Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the
068 * Appender can use the Layout. For example, it might concatenate information in front or behind the
069 * data and then write it all to the OutputStream in one call.
070 */
071 byte[] toByteArray(LogEvent event);
072
073 /**
074 * Formats the event as an Object that can be serialized.
075 *
076 * @param event The Logging Event.
077 * @return The formatted event.
078 */
079 T toSerializable(LogEvent event);
080
081 /**
082 * Returns the content type output by this layout. The base class returns "text/plain".
083 *
084 * @return the content type.
085 */
086 String getContentType();
087
088 /**
089 * Returns a description of the content format.
090 *
091 * @return a Map of key/value pairs describing the Layout-specific content format, or an empty Map if no content format descriptors are specified.
092 *
093 */
094 Map<String, String> getContentFormat();
095 }