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 21 /** 22 * Lays out a {@linkplain LogEvent} in different formats. 23 * 24 * The formats are: 25 * <ul> 26 * <li> 27 * {@code byte[]}</li> 28 * <li> 29 * an implementor of {@linkplain Serializable}, like {@code byte[]}</li> 30 * <li> 31 * {@linkplain String}</li> 32 * <li> 33 * {@linkplain LogEvent}</li> 34 * </ul> 35 * 36 * @param <T> 37 * The {@link Serializable} type returned by {@link #toSerializable(LogEvent)} 38 * 39 * @doubt There is still a need for a character-based layout for character based event sinks (databases, etc). Would 40 * introduce an EventEncoder, EventRenderer or something similar for the logging event to byte encoding. (RG) A layout 41 * can be configured with a Charset and then Strings can be converted to byte arrays. OTOH, it isn't possible to write 42 * byte arrays as character streams. 43 */ 44 public interface Layout<T extends Serializable> { 45 /** 46 * Returns the format for the layout format. 47 * @return The footer. 48 * @doubt the concept of header and footer is not universal, should not be on the base interface. 49 * (RG) I agree with this. 50 */ 51 byte[] getFooter(); 52 53 /** 54 * Returns the header for the layout format. 55 * @return The header. 56 * @doubt the concept of header and footer is not universal, should not be on the base interface. 57 * (RG) I agree with this. 58 */ 59 byte[] getHeader(); 60 61 /** 62 * Formats the event suitable for display. 63 * 64 * @param event The Logging Event. 65 * @return The formatted event. 66 * @doubt Likely better to write to a OutputStream instead of return a byte[]. (RG) That limits how the 67 * Appender can use the Layout. For example, it might concatenate information in front or behind the 68 * data and then write it all to the OutputStream in one call. 69 */ 70 byte[] toByteArray(LogEvent event); 71 72 /** 73 * Formats the event as an Object that can be serialized. 74 * 75 * @param event The Logging Event. 76 * @return The formatted event. 77 */ 78 T toSerializable(LogEvent event); 79 80 /** 81 * Returns the content type output by this layout. The base class returns "text/plain". 82 * 83 * @return the content type. 84 */ 85 String getContentType(); 86 }