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 */ 017package org.apache.logging.log4j.message; 018 019import java.io.Serializable; 020import org.apache.logging.log4j.util.StringBuilderFormattable; 021 022/** 023 * An interface for various Message implementations that can be logged. Messages can act as wrappers 024 * around Objects so that user can have control over converting Objects to Strings when necessary without 025 * requiring complicated formatters and as a way to manipulate the message based on information available 026 * at runtime such as the locale of the system. 027 * <p> 028 * Custom Message implementations should consider implementing the {@link StringBuilderFormattable} 029 * interface for more efficient processing. Garbage-free Layouts will call 030 * {@link StringBuilderFormattable#formatTo(StringBuilder) formatTo(StringBuilder)} instead of 031 * {@link Message#getFormattedMessage()} if the Message implements StringBuilderFormattable. 032 * </p> 033 * <p> 034 * Note: Message objects should not be considered to be thread safe nor should they be assumed to be 035 * safely reusable even on the same thread. The logging system may provide information to the Message 036 * objects and the Messages might be queued for asynchronous delivery. Thus, any modifications to a 037 * Message object by an application should by avoided after the Message has been passed as a parameter on 038 * a Logger method. 039 * </p> 040 * 041 * @see StringBuilderFormattable 042 */ 043/* 044 * Implementation note: this interface extends Serializable since LogEvents must be serializable. 045 */ 046public interface Message extends Serializable { 047 048 /** 049 * Gets the Message formatted as a String. Each Message implementation determines the 050 * appropriate way to format the data encapsulated in the Message. Messages that provide 051 * more than one way of formatting the Message will implement MultiformatMessage. 052 * <p> 053 * When configured to log asynchronously, this method is called before the Message is queued, unless this 054 * message implements {@link ReusableMessage} or is annotated with {@link AsynchronouslyFormattable}. 055 * This gives the Message implementation class a chance to create a formatted message String with the current value 056 * of any mutable objects. 057 * The intention is that the Message implementation caches this formatted message and returns it on subsequent 058 * calls. (See <a href="https://issues.apache.org/jira/browse/LOG4J2-763">LOG4J2-763</a>.) 059 * </p> 060 * <p> 061 * When logging synchronously, this method will not be called for Messages that implement the 062 * {@link StringBuilderFormattable} interface: instead, the 063 * {@link StringBuilderFormattable#formatTo(StringBuilder) formatTo(StringBuilder)} method will be called so the 064 * Message can format its contents without creating intermediate String objects. 065 * </p> 066 * 067 * @return The message String. 068 */ 069 String getFormattedMessage(); 070 071 /** 072 * Gets the format portion of the Message. 073 * 074 * @return The message format. Some implementations, such as ParameterizedMessage, will use this as 075 * the message "pattern". Other Messages may simply return an empty String. 076 * TODO Do all messages have a format? What syntax? Using a Formatter object could be cleaner. 077 * (RG) In SimpleMessage the format is identical to the formatted message. In ParameterizedMessage and 078 * StructuredDataMessage it is not. It is up to the Message implementer to determine what this 079 * method will return. A Formatter is inappropriate as this is very specific to the Message 080 * implementation so it isn't clear to me how having a Formatter separate from the Message would be cleaner. 081 */ 082 String getFormat(); 083 084 /** 085 * Gets parameter values, if any. 086 * 087 * @return An array of parameter values or null. 088 */ 089 Object[] getParameters(); 090 091 /** 092 * Gets the throwable, if any. 093 * 094 * @return the throwable or null. 095 */ 096 Throwable getThrowable(); 097}