blob: a5924b4156e0097e92331b91c6579548e5f6f80b [file] [log] [blame]
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +02001package eu.mulk.quarkus.googlecloud.jsonlogging;
2
3import java.math.BigDecimal;
4import java.math.BigInteger;
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +02005import java.util.Objects;
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +02006import javax.json.Json;
7import javax.json.JsonObjectBuilder;
8import javax.json.JsonValue;
9
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020010/**
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020011 * A simple single key–value pair forming a {@link StructuredParameter}.
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020012 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020013 * <p>This class is suitable for the common case of logging a key–value pair as parameter to the
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020014 * {@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 Benkard42da9f12021-09-02 18:47:28 +020017 * <p><strong>Example:</strong>
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020018 *
19 * <pre>{@code
20 * logger.infof("Application starting.", StructuredParameter.of("version", "1.0"));
21 * }</pre>
22 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020023 * Result:
24 *
25 * <pre>{@code
26 * {
27 * "jsonPayload": {
28 * "message": "Application starting.",
29 * "version": "1.0"
30 * }
31 * }
32 * }</pre>
33 *
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020034 * @see Label
35 * @see StructuredParameter
36 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020037public 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 Benkardb8fbc372021-05-11 06:50:45 +020046
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020047 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020056 public static KeyValueParameter of(String key, String value) {
57 return new KeyValueParameter(key, Json.createValue(value));
58 }
59
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020060 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020069 public static KeyValueParameter of(String key, int value) {
70 return new KeyValueParameter(key, Json.createValue(value));
71 }
72
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020073 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020082 public static KeyValueParameter of(String key, long value) {
83 return new KeyValueParameter(key, Json.createValue(value));
84 }
85
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020086 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020095 public static KeyValueParameter of(String key, double value) {
96 return new KeyValueParameter(key, Json.createValue(value));
97 }
98
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020099 /**
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 Benkardb8fbc372021-05-11 06:50:45 +0200108 public static KeyValueParameter of(String key, BigDecimal value) {
109 return new KeyValueParameter(key, Json.createValue(value));
110 }
111
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200112 /**
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 Benkardb8fbc372021-05-11 06:50:45 +0200121 public static KeyValueParameter of(String key, BigInteger value) {
122 return new KeyValueParameter(key, Json.createValue(value));
123 }
124
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200125 /**
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 Benkardb8fbc372021-05-11 06:50:45 +0200134 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 Benkard121a6312021-05-12 05:41:25 +0200142
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200143 /**
144 * The key part of the key–value pair.
145 *
146 * @return the key part of the key–value pair.
147 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +0200148 public String key() {
149 return key;
150 }
151
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200152 /**
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 Benkard121a6312021-05-12 05:41:25 +0200160 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 Benkardb8fbc372021-05-11 06:50:45 +0200181}