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 * 41 * @see StringBuilderFormattable 42 */ 43 /* 44 * Implementation note: this interface extends Serializable since LogEvents must be serializable. 45 */ 46 public interface Message extends Serializable { 47 48 /** 49 * Gets the Message formatted as a String. Each Message implementation determines the 50 * appropriate way to format the data encapsulated in the Message. Messages that provide 51 * more than one way of formatting the Message will implement MultiformatMessage. 52 * <p> 53 * When configured to log asynchronously, this method is called before the Message is queued, unless this 54 * message implements {@link ReusableMessage} or is annotated with {@link AsynchronouslyFormattable}. 55 * This gives the Message implementation class a chance to create a formatted message String with the current value 56 * of any mutable objects. 57 * The intention is that the Message implementation caches this formatted message and returns it on subsequent 58 * calls. (See <a href="https://issues.apache.org/jira/browse/LOG4J2-763">LOG4J2-763</a>.) 59 * </p> 60 * <p> 61 * When logging synchronously, this method will not be called for Messages that implement the 62 * {@link StringBuilderFormattable} interface: instead, the 63 * {@link StringBuilderFormattable#formatTo(StringBuilder) formatTo(StringBuilder)} method will be called so the 64 * Message can format its contents without creating intermediate String objects. 65 * </p> 66 * 67 * @return The message String. 68 */ 69 String getFormattedMessage(); 70 71 /** 72 * Gets the format portion of the Message. 73 * 74 * @return The message format. Some implementations, such as ParameterizedMessage, will use this as 75 * the message "pattern". Other Messages may simply return an empty String. 76 * TODO Do all messages have a format? What syntax? Using a Formatter object could be cleaner. 77 * (RG) In SimpleMessage the format is identical to the formatted message. In ParameterizedMessage and 78 * StructuredDataMessage it is not. It is up to the Message implementer to determine what this 79 * method will return. A Formatter is inappropriate as this is very specific to the Message 80 * implementation so it isn't clear to me how having a Formatter separate from the Message would be cleaner. 81 */ 82 String getFormat(); 83 84 /** 85 * Gets parameter values, if any. 86 * 87 * @return An array of parameter values or null. 88 */ 89 Object[] getParameters(); 90 91 /** 92 * Gets the throwable, if any. 93 * 94 * @return the throwable or null. 95 */ 96 Throwable getThrowable(); 97 }