org.apache.logging.log4j.message

Class ParameterizedMessage

  • All Implemented Interfaces:
    Serializable, Message


    public class ParameterizedMessage
    extends Object
    implements Message
    Handles messages that consist of a format string containing '{}' to represent each replaceable token, and the parameters.

    This class was originally written for Lilith by Joern Huxhorn where it is licensed under the LGPL. It has been relicensed here with his permission providing that this attribution remain.

    See Also:
    Serialized Form
    • Constructor Detail

      • ParameterizedMessage

        public ParameterizedMessage(String messagePattern,
                            String[] stringArgs,
                            Throwable throwable)
        Creates a parameterized message.
        Parameters:
        messagePattern - The message "format" string. This will be a String containing "{}" placeholders where parameters should be substituted.
        stringArgs - The arguments for substitution.
        throwable - A Throwable.
      • ParameterizedMessage

        public ParameterizedMessage(String messagePattern,
                            Object[] objectArgs,
                            Throwable throwable)
        Creates a parameterized message.
        Parameters:
        messagePattern - The message "format" string. This will be a String containing "{}" placeholders where parameters should be substituted.
        objectArgs - The arguments for substitution.
        throwable - A Throwable.
      • ParameterizedMessage

        public ParameterizedMessage(String messagePattern,
                            Object[] arguments)
        Constructs a ParameterizedMessage which contains the arguments converted to String as well as an optional Throwable.

        If the last argument is a Throwable and is NOT used up by a placeholder in the message pattern it is returned in getThrowable() and won't be contained in the created String[]. If it is used up getThrowable() will return null even if the last argument was a Throwable!

        Parameters:
        messagePattern - the message pattern that to be checked for placeholders.
        arguments - the argument array to be converted.
      • ParameterizedMessage

        public ParameterizedMessage(String messagePattern,
                            Object arg)
        Constructor with a pattern and a single parameter.
        Parameters:
        messagePattern - The message pattern.
        arg - The parameter.
      • ParameterizedMessage

        public ParameterizedMessage(String messagePattern,
                            Object arg1,
                            Object arg2)
        Constructor with a pattern and two parameters.
        Parameters:
        messagePattern - The message pattern.
        arg1 - The first parameter.
        arg2 - The second parameter.
    • Method Detail

      • getFormattedMessage

        public String getFormattedMessage()
        Returns the formatted message.
        Specified by:
        getFormattedMessage in interface Message
        Returns:
        the formatted message.
      • getFormat

        public String getFormat()
        Returns the message pattern.
        Specified by:
        getFormat in interface Message
        Returns:
        the message pattern.
      • getParameters

        public Object[] getParameters()
        Returns the message parameters.
        Specified by:
        getParameters in interface Message
        Returns:
        the message parameters.
      • getThrowable

        public Throwable getThrowable()
        Returns the Throwable that was given as the last argument, if any. It will not survive serialization. The Throwable exists as part of the message primarily so that it can be extracted from the end of the list of parameters and then be added to the LogEvent. As such, the Throwable in the event should not be used once the LogEvent has been constructed.
        Specified by:
        getThrowable in interface Message
        Returns:
        the Throwable, if any.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • format

        public static String format(String messagePattern,
                    Object[] arguments)
        Replace placeholders in the given messagePattern with arguments.
        Parameters:
        messagePattern - the message pattern containing placeholders.
        arguments - the arguments to be used to replace placeholders.
        Returns:
        the formatted message.
      • countArgumentPlaceholders

        public static int countArgumentPlaceholders(String messagePattern)
        Counts the number of unescaped placeholders in the given messagePattern.
        Parameters:
        messagePattern - the message pattern to be analyzed.
        Returns:
        the number of unescaped placeholders.
      • deepToString

        public static String deepToString(Object o)
        This method performs a deep toString of the given Object. Primitive arrays are converted using their respective Arrays.toString methods while special handling is implemented for "container types", i.e. Object[], Map and Collection because those could contain themselves.

        It should be noted that neither AbstractMap.toString() nor AbstractCollection.toString() implement such a behavior. They only check if the container is directly contained in itself, but not if a contained container contains the original one. Because of that, Arrays.toString(Object[]) isn't safe either. Confusing? Just read the last paragraph again and check the respective toString() implementation.

        This means, in effect, that logging would produce a usable output even if an ordinary System.out.println(o) would produce a relatively hard-to-debug StackOverflowError.

        Parameters:
        o - The object.
        Returns:
        The String representation.
      • identityToString

        public static String identityToString(Object obj)
        This method returns the same as if Object.toString() would not have been overridden in obj.

        Note that this isn't 100% secure as collisions can always happen with hash codes.

        Copied from Object.hashCode():

        As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
        Parameters:
        obj - the Object that is to be converted into an identity string.
        Returns:
        the identity string as also defined in Object.toString()

Copyright © 1999-2015 Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.