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.message; 18 19 import java.io.Serializable; 20 import org.apache.logging.log4j.util.StringBuilderFormattable; 21 22 /** 23 * An interface for various Message implementations that can be logged. Messages can act as wrappers 24 * around Objects so that user can have control over converting Objects to Strings when necessary without 25 * requiring complicated formatters and as a way to manipulate the message based on information available 26 * at runtime such as the locale of the system. 27 * <p> 28 * Custom Message implementations should consider implementing the {@link StringBuilderFormattable} 29 * interface for more efficient processing. Garbage-free Layouts will call 30 * {@link StringBuilderFormattable#formatTo(StringBuilder) formatTo(StringBuilder)} instead of 31 * {@link Message#getFormattedMessage()} if the Message implements StringBuilderFormattable. 32 * </p> 33 *<p> 34 * Note: Message objects should not be considered to be thread safe nor should they be assumed to be 35 * safely reusable even on the same thread. The logging system may provide information to the Message 36 * objects and the Messages might be queued for asynchronous delivery. Thus, any modifications to a 37 * Message object by an application should by avoided after the Message has been passed as a parameter on 38 * a Logger method. 39 * </p> 40 * TODO Interfaces should rarely extend Serializable according to Effective Java 2nd Ed pg 291. 41 * (RG) That section also says "If a class or interface exists primarily to participate in a framework that 42 * requires all participants to implement Serializable, then it makes perfect sense for the class or 43 * interface to implement or extend Serializable". Such is the case here as the LogEvent must be Serializable. 44 * 45 * @see StringBuilderFormattable 46 */ 47 public interface Message extends Serializable { 48 49 /** 50 * Gets the Message formatted as a String. Each Message implementation determines the 51 * appropriate way to format the data encapsulated in the Message. Messages that provide 52 * more than one way of formatting the Message will implement MultiformatMessage. 53 * <p> 54 * This method will not be called for Messages that implement the 55 * {@link StringBuilderFormattable} interface: instead, the 56 * {@link StringBuilderFormattable#formatTo(StringBuilder) formatTo(StringBuilder)} method will be called so the 57 * Message can format its contents without creating intermediate String objects. 58 * </p> 59 * 60 * @return The message String. 61 */ 62 String getFormattedMessage(); 63 64 /** 65 * Gets the format portion of the Message. 66 * 67 * @return The message format. Some implementations, such as ParameterizedMessage, will use this as 68 * the message "pattern". Other Messages may simply return an empty String. 69 * TODO Do all messages have a format? What syntax? Using a Formatter object could be cleaner. 70 * (RG) In SimpleMessage the format is identical to the formatted message. In ParameterizedMessage and 71 * StructuredDataMessage it is not. It is up to the Message implementer to determine what this 72 * method will return. A Formatter is inappropriate as this is very specific to the Message 73 * implementation so it isn't clear to me how having a Formatter separate from the Message would be cleaner. 74 */ 75 String getFormat(); 76 77 /** 78 * Gets parameter values, if any. 79 * 80 * @return An array of parameter values or null. 81 */ 82 Object[] getParameters(); 83 84 /** 85 * Gets the throwable, if any. 86 * 87 * @return the throwable or null. 88 */ 89 Throwable getThrowable(); 90 }