Class Tags


  • public final class Tags
    extends Object
    Immutable tags which can be attached to log statements via platform specific injection mechanisms.

    A tag is either a "simple" tag, added via Tags.Builder.addTag(String) or a tag with a value, added via one of the addTag(name,value) methods. When thinking of tags as a Map<String, Set<Object>>, the value of a "simple" tag is the empty set.

    Tag values can be of several simple types and are held in a stable, sorted order within a Tags instance. In other words it never matters in which order two Tags instances are merged.

    When tags are merged, the result is the union of the values. This is easier to explain When thinking of tags as a Map<String, Set<Object>>, where "merging" means taking the union of the Set associated with the tag name. In particular, for a given tag name:

    • Adding the same value for a given tag twice has no additional effect.
    • Adding a simple tag twice has no additional effect.
    • Adding a tag with a value is also implicitly like adding a simple tag with the same name.

    The toString() implementation of this class provides a human readable, machine parsable representation of the tags.

    • Method Detail

      • builder

        public static Tags.Builder builder()
        Returns a new builder for adding tags.
      • empty

        public static Tags empty()
        Returns the immutable empty tags instance.
      • of

        public static Tags of​(String name,
                              String value)
        Returns a single tag without needing to use the builder API. Where multiple tags are needed, it is always better to use the builder directly.
      • of

        public static Tags of​(String name,
                              boolean value)
        Returns a single tag without needing to use the builder API. Where multiple tags are needed, it is always better to use the builder directly.
      • of

        public static Tags of​(String name,
                              long value)
        Returns a single tag without needing to use the builder API. Where multiple tags are needed, it is always better to use the builder directly.
      • of

        public static Tags of​(String name,
                              double value)
        Returns a single tag without needing to use the builder API. Where multiple tags are needed, it is always better to use the builder directly.
      • asMap

        public Map<String,​Set<Object>> asMap()
        Returns an immutable map containing the tag values.
      • isEmpty

        public boolean isEmpty()
        Returns whether this instance is empty.
      • merge

        public Tags merge​(Tags other)
        Merges two tags instances, combining values for any name contained in both.
      • equals

        public boolean equals​(@Nullable Object obj)
        Overrides:
        equals in class Object
      • hashCode

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

        public String toString()
        Returns human readable representation of the tags. This is not a stable representation and may change over time. If you need to format tags reliably for logging, you should not rely on this method.
        Overrides:
        toString in class Object