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;
020
021/**
022 * An interface for various Message implementations that can be logged. Messages can act as wrappers
023 * around Objects so that user can have control over converting Objects to Strings when necessary without
024 * requiring complicated formatters and as a way to manipulate the message based on information available
025 * at runtime such as the locale of the system.
026 *<p>
027 * Note: Message objects should not be considered to be thread safe nor should they be assumed to be
028 * safely reusable even on the same thread. The logging system may provide information to the Message
029 * objects and the Messages might be queued for asynchronous delivery. Thus, any modifications to a
030 * Message object by an application should by avoided after the Message has been passed as a parameter on
031 * a Logger method.
032 * </p>
033 * TODO Interfaces should rarely extend Serializable according to Effective Java 2nd Ed pg 291.
034 * (RG) That section also says "If a class or interface exists primarily to participate in a framework that
035 * requires all participants to implement Serializable, then it makes perfect sense for the class or
036 * interface to implement or extend Serializable". Such is the case here as the LogEvent must be Serializable.
037 */
038public interface Message extends Serializable {
039
040    /**
041     * Gets the Message formatted as a String. Each Message implementation determines the
042     * appropriate way to format the data encapsulated in the Message. Messages that provide
043     * more than one way of formatting the Message will implement MultiformatMessage.
044     *
045     * @return The message String.
046     */
047    String getFormattedMessage();
048
049    /**
050     * Gets the format portion of the Message.
051     *
052     * @return The message format. Some implementations, such as ParameterizedMessage, will use this as
053     * the message "pattern". Other Messages may simply return an empty String.
054     * TODO Do all messages have a format?  What syntax?  Using a Formatter object could be cleaner.
055     * (RG) In SimpleMessage the format is identical to the formatted message. In ParameterizedMessage and
056     * StructuredDataMessage it is not. It is up to the Message implementer to determine what this
057     * method will return. A Formatter is inappropriate as this is very specific to the Message
058     * implementation so it isn't clear to me how having a Formatter separate from the Message would be cleaner.
059     */
060    String getFormat();
061
062    /**
063     * Gets parameter values, if any.
064     *
065     * @return An array of parameter values or null.
066     */
067    Object[] getParameters();
068
069    /**
070     * Gets the throwable, if any.
071     *
072     * @return the throwable or null.
073     */
074    Throwable getThrowable();
075}