summaryrefslogtreecommitdiff
path: root/core/src/main/java/eu/mulk/quarkus/googlecloud/jsonlogging/Label.java
blob: d5a900032daa0284df110e912bb6eac5aa13bdc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
//
// SPDX-License-Identifier: LGPL-3.0-or-later

package eu.mulk.quarkus.googlecloud.jsonlogging;

import java.util.Objects;

/**
 * A label usable to tag a log message.
 *
 * <p>Instances of {@link Label} can be passed as log parameters to the {@code *f} family of logging
 * functions on {@link org.jboss.logging.Logger}.
 *
 * <p><strong>Example:</strong>
 *
 * {@snippet :
 * logger.logf("Request rejected: unauthorized.", Label.of("requestId", "123"));
 * }
 *
 * <p>Result:
 *
 * {@snippet lang="json" :
 * {
 *   "textPayload": "Request rejected: unauthorized.",
 *   "labels": {
 *     "requestId": "123"
 *   }
 * }
 * }
 *
 * @see KeyValueParameter
 * @see StructuredParameter
 */
public final class Label {

  private final String key;
  private final String value;

  private Label(String key, String value) {
    this.key = key;
    this.value = value;
  }

  /**
   * Constructs a {@link Label} from a key (i.e. name) and a value.
   *
   * <p>It is often useful for the key to be a {@link String} constant that is shared by multiple
   * parts of the program.
   *
   * @param key the key (name) of the label.
   * @param value the value of the label.
   * @return the newly constructed {@link Label}, ready to be passed to a logging function.
   */
  public static Label of(String key, String value) {
    return new Label(key, value);
  }

  /**
   * The name of the label.
   *
   * <p>It is often useful for this to be a {@link String} constant that is shared by multiple parts
   * of the program.
   *
   * @return the name of the label.
   */
  public String key() {
    return key;
  }

  /**
   * The value of the label.
   *
   * @return the value of the label.
   */
  public String value() {
    return value;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) return true;
    if (obj == null || obj.getClass() != this.getClass()) return false;
    var that = (Label) obj;
    return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value);
  }

  @Override
  public int hashCode() {
    return Objects.hash(key, value);
  }

  @Override
  public String toString() {
    return "Label[" + "key=" + key + ", " + "value=" + value + ']';
  }
}