blob: 33664ddcdd6d7102e5fdbfb42ab1830c250678b0 [file] [log] [blame]
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +02001package eu.mulk.quarkus.googlecloud.jsonlogging;
2
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +02003import java.util.Objects;
4
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +02005/**
6 * A label usable to tag a log message.
7 *
8 * <p>Instances of {@link Label} can be passed as log parameters to the {@code *f} family of logging
9 * functions on {@link org.jboss.logging.Logger}.
10 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020011 * <p><strong>Example:</strong>
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020012 *
13 * <pre>{@code
14 * logger.logf("Request rejected: unauthorized.", Label.of("requestId", "123"));
15 * }</pre>
16 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020017 * Result:
18 *
19 * <pre>{@code
20 * {
21 * "textPayload": "Request rejected: unauthorized.",
22 * "labels": {
23 * "requestId": "123"
24 * }
25 * }
26 * }</pre>
27 *
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020028 * @see KeyValueParameter
29 * @see StructuredParameter
30 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020031public final class Label {
32
33 private final String key;
34 private final String value;
35
36 private Label(String key, String value) {
37 this.key = key;
38 this.value = value;
39 }
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +020040
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020041 /**
42 * Constructs a {@link Label} from a key (i.e. name) and a value.
43 *
44 * <p>It is often useful for the key to be a {@link String} constant that is shared by multiple
45 * parts of the program.
46 *
47 * @param key the key (name) of the label.
48 * @param value the value of the label.
49 * @return the newly constructed {@link Label}, ready to be passed to a logging function.
50 */
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +020051 public static Label of(String key, String value) {
52 return new Label(key, value);
53 }
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020054
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020055 /**
56 * The name of the label.
57 *
58 * <p>It is often useful for this to be a {@link String} constant that is shared by multiple parts
59 * of the program.
60 *
61 * @return the name of the label.
62 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020063 public String key() {
64 return key;
65 }
66
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020067 /**
68 * The value of the label.
69 *
70 * @return the value of the label.
71 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020072 public String value() {
73 return value;
74 }
75
76 @Override
77 public boolean equals(Object obj) {
78 if (obj == this) return true;
79 if (obj == null || obj.getClass() != this.getClass()) return false;
80 var that = (Label) obj;
81 return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value);
82 }
83
84 @Override
85 public int hashCode() {
86 return Objects.hash(key, value);
87 }
88
89 @Override
90 public String toString() {
91 return "Label[" + "key=" + key + ", " + "value=" + value + ']';
92 }
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +020093}