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