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 | |
| 7 | import java.math.BigDecimal; |
| 8 | import java.math.BigInteger; |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 9 | import java.util.Objects; |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 10 | import javax.json.Json; |
| 11 | import javax.json.JsonObjectBuilder; |
| 12 | import javax.json.JsonValue; |
| 13 | |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 14 | /** |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 15 | * A simple single key–value pair forming a {@link StructuredParameter}. |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 16 | * |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 17 | * <p>This class is suitable for the common case of logging a key–value pair as parameter to the |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 18 | * {@code *f} family of logging functions on {@link org.jboss.logging.Logger}. For advanced use |
| 19 | * cases, provide your own implementation of {@link StructuredParameter}. |
| 20 | * |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 21 | * <p><strong>Example:</strong> |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 22 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 23 | * {@snippet : |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 24 | * logger.infof("Application starting.", StructuredParameter.of("version", "1.0")); |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 25 | * } |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 26 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 27 | * <p>Result: |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 28 | * |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 29 | * {@snippet lang="json" : |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 30 | * { |
| 31 | * "jsonPayload": { |
| 32 | * "message": "Application starting.", |
| 33 | * "version": "1.0" |
| 34 | * } |
| 35 | * } |
Matthias Andreas Benkard | e369c51 | 2022-04-15 20:54:52 +0200 | [diff] [blame] | 36 | * } |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 37 | * |
Matthias Andreas Benkard | 692f48d | 2021-08-31 21:06:50 +0200 | [diff] [blame] | 38 | * @see Label |
| 39 | * @see StructuredParameter |
| 40 | */ |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 41 | public final class KeyValueParameter implements StructuredParameter { |
| 42 | |
| 43 | private final String key; |
| 44 | private final JsonValue value; |
| 45 | |
| 46 | private KeyValueParameter(String key, JsonValue value) { |
| 47 | this.key = key; |
| 48 | this.value = value; |
| 49 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 50 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 51 | /** |
| 52 | * Creates a {@link KeyValueParameter} from a {@link String} value. |
| 53 | * |
| 54 | * <p>The resulting JSON value is of type {@code string}. |
| 55 | * |
| 56 | * @param key the key part of the key–value pair. |
| 57 | * @param value the value part of the key–value pair. |
| 58 | * @return the newly constructed parameter, ready to be passed to a logging function. |
| 59 | */ |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 60 | public static KeyValueParameter of(String key, String value) { |
| 61 | return new KeyValueParameter(key, Json.createValue(value)); |
| 62 | } |
| 63 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 64 | /** |
| 65 | * Creates a {@link KeyValueParameter} from an {@code int} value. |
| 66 | * |
| 67 | * <p>The resulting JSON value is of type {@code number}. |
| 68 | * |
| 69 | * @param key the key part of the key–value pair. |
| 70 | * @param value the value part of the key–value pair. |
| 71 | * @return the newly constructed parameter, ready to be passed to a logging function. |
| 72 | */ |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 73 | public static KeyValueParameter of(String key, int value) { |
| 74 | return new KeyValueParameter(key, Json.createValue(value)); |
| 75 | } |
| 76 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Creates a {@link KeyValueParameter} from a {@code long} value. |
| 79 | * |
| 80 | * <p>The resulting JSON value is of type {@code number}. |
| 81 | * |
| 82 | * @param key the key part of the key–value pair. |
| 83 | * @param value the value part of the key–value pair. |
| 84 | * @return the newly constructed parameter, ready to be passed to a logging function. |
| 85 | */ |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 86 | public static KeyValueParameter of(String key, long value) { |
| 87 | return new KeyValueParameter(key, Json.createValue(value)); |
| 88 | } |
| 89 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 90 | /** |
| 91 | * Creates a {@link KeyValueParameter} from a {@code double} value. |
| 92 | * |
| 93 | * <p>The resulting JSON value is of type {@code number}. |
| 94 | * |
| 95 | * @param key the key part of the key–value pair. |
| 96 | * @param value the value part of the key–value pair. |
| 97 | * @return the newly constructed parameter, ready to be passed to a logging function. |
| 98 | */ |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 99 | public static KeyValueParameter of(String key, double value) { |
| 100 | return new KeyValueParameter(key, Json.createValue(value)); |
| 101 | } |
| 102 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 103 | /** |
| 104 | * Creates a {@link KeyValueParameter} from a {@link BigDecimal} value. |
| 105 | * |
| 106 | * <p>The resulting JSON value is of type {@code number}. |
| 107 | * |
| 108 | * @param key the key part of the key–value pair. |
| 109 | * @param value the value part of the key–value pair. |
| 110 | * @return the newly constructed parameter, ready to be passed to a logging function. |
| 111 | */ |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 112 | public static KeyValueParameter of(String key, BigDecimal value) { |
| 113 | return new KeyValueParameter(key, Json.createValue(value)); |
| 114 | } |
| 115 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 116 | /** |
| 117 | * Creates a {@link KeyValueParameter} from a {@link BigInteger} value. |
| 118 | * |
| 119 | * <p>The resulting JSON value is of type {@code number}. |
| 120 | * |
| 121 | * @param key the key part of the key–value pair. |
| 122 | * @param value the value part of the key–value pair. |
| 123 | * @return the newly constructed parameter, ready to be passed to a logging function. |
| 124 | */ |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 125 | public static KeyValueParameter of(String key, BigInteger value) { |
| 126 | return new KeyValueParameter(key, Json.createValue(value)); |
| 127 | } |
| 128 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 129 | /** |
| 130 | * Creates a {@link KeyValueParameter} from a {@code boolean} value. |
| 131 | * |
| 132 | * <p>The resulting JSON value is of type {@code boolean}. |
| 133 | * |
| 134 | * @param key the key part of the key–value pair. |
| 135 | * @param value the value part of the key–value pair. |
| 136 | * @return the newly constructed parameter, ready to be passed to a logging function. |
| 137 | */ |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 138 | public static KeyValueParameter of(String key, boolean value) { |
| 139 | return new KeyValueParameter(key, value ? JsonValue.TRUE : JsonValue.FALSE); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public JsonObjectBuilder json() { |
| 144 | return Json.createObjectBuilder().add(key, value); |
| 145 | } |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 146 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 147 | /** |
| 148 | * The key part of the key–value pair. |
| 149 | * |
| 150 | * @return the key part of the key–value pair. |
| 151 | */ |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 152 | public String key() { |
| 153 | return key; |
| 154 | } |
| 155 | |
Matthias Andreas Benkard | 42da9f1 | 2021-09-02 18:47:28 +0200 | [diff] [blame] | 156 | /** |
| 157 | * The value part of the key–value pair. |
| 158 | * |
| 159 | * <p>Can be of any non-composite JSON type (i.e. {@code string}, {@code number}, or {@code |
| 160 | * boolean}). |
| 161 | * |
| 162 | * @return the value pairt of the key–value pair. |
| 163 | */ |
Matthias Andreas Benkard | 121a631 | 2021-05-12 05:41:25 +0200 | [diff] [blame] | 164 | public JsonValue value() { |
| 165 | return value; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean equals(Object obj) { |
| 170 | if (obj == this) return true; |
| 171 | if (obj == null || obj.getClass() != this.getClass()) return false; |
| 172 | var that = (KeyValueParameter) obj; |
| 173 | return Objects.equals(this.key, that.key) && Objects.equals(this.value, that.value); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int hashCode() { |
| 178 | return Objects.hash(key, value); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public String toString() { |
| 183 | return "KeyValueParameter[" + "key=" + key + ", " + "value=" + value + ']'; |
| 184 | } |
Matthias Andreas Benkard | b8fbc37 | 2021-05-11 06:50:45 +0200 | [diff] [blame] | 185 | } |