blob: d5a900032daa0284df110e912bb6eac5aa13bdc9 [file] [log] [blame]
Matthias Andreas Benkard80909242022-02-03 20:47:47 +01001// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +02005package eu.mulk.quarkus.googlecloud.jsonlogging;
6
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +02007import java.util.Objects;
8
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +02009/**
10 * A label usable to tag a log message.
11 *
12 * <p>Instances of {@link Label} can be passed as log parameters to the {@code *f} family of logging
13 * functions on {@link org.jboss.logging.Logger}.
14 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020015 * <p><strong>Example:</strong>
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020016 *
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020017 * {@snippet :
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020018 * logger.logf("Request rejected: unauthorized.", Label.of("requestId", "123"));
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020019 * }
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020020 *
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020021 * <p>Result:
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020022 *
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020023 * {@snippet lang="json" :
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020024 * {
25 * "textPayload": "Request rejected: unauthorized.",
26 * "labels": {
27 * "requestId": "123"
28 * }
29 * }
Matthias Andreas Benkarde369c512022-04-15 20:54:52 +020030 * }
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020031 *
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020032 * @see KeyValueParameter
33 * @see StructuredParameter
34 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020035public final class Label {
36
37 private final String key;
38 private final String value;
39
40 private Label(String key, String value) {
41 this.key = key;
42 this.value = value;
43 }
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +020044
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020045 /**
46 * Constructs a {@link Label} from a key (i.e. name) and a value.
47 *
48 * <p>It is often useful for the key to be a {@link String} constant that is shared by multiple
49 * parts of the program.
50 *
51 * @param key the key (name) of the label.
52 * @param value the value of the label.
53 * @return the newly constructed {@link Label}, ready to be passed to a logging function.
54 */
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +020055 public static Label of(String key, String value) {
56 return new Label(key, value);
57 }
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020058
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020059 /**
60 * The name of the label.
61 *
62 * <p>It is often useful for this to be a {@link String} constant that is shared by multiple parts
63 * of the program.
64 *
65 * @return the name of the label.
66 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020067 public String key() {
68 return key;
69 }
70
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020071 /**
72 * The value of the label.
73 *
74 * @return the value of the label.
75 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020076 public String value() {
77 return value;
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (obj == this) return true;
83 if (obj == null || obj.getClass() != this.getClass()) return false;
84 var that = (Label) obj;
85 return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value);
86 }
87
88 @Override
89 public int hashCode() {
90 return Objects.hash(key, value);
91 }
92
93 @Override
94 public String toString() {
95 return "Label[" + "key=" + key + ", " + "value=" + value + ']';
96 }
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +020097}