Matthias Andreas Benkard | 8090924 | 2022-02-03 20:47:47 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de> |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 5 | package eu.mulk.quarkus.googlecloud.jsonlogging; |
| 6 | |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 7 | import java.util.Objects; |
| 8 | |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 9 | /** |
| 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 Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 15 | * <p><strong>Example:</strong> |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 16 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 17 | * {@snippet : |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 18 | * logger.logf("Request rejected: unauthorized.", Label.of("requestId", "123")); |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 19 | * } |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 20 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 21 | * <p>Result: |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 22 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 23 | * {@snippet lang="json" : |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 24 | * { |
| 25 | * "textPayload": "Request rejected: unauthorized.", |
| 26 | * "labels": { |
| 27 | * "requestId": "123" |
| 28 | * } |
| 29 | * } |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 30 | * } |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 31 | * |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 32 | * @see KeyValueParameter |
| 33 | * @see StructuredParameter |
| 34 | */ |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 35 | public 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 Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 44 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 45 | /** |
| 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 Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 55 | public static Label of(String key, String value) { |
| 56 | return new Label(key, value); |
| 57 | } |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 58 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 59 | /** |
| 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 Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 67 | public String key() { |
| 68 | return key; |
| 69 | } |
| 70 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 71 | /** |
| 72 | * The value of the label. |
| 73 | * |
| 74 | * @return the value of the label. |
| 75 | */ |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 76 | 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 Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 97 | } |