View Javadoc

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