blob: d8ab58dd5a0702890f8e20ed2921c7fdf9a0863f [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
7import java.math.BigDecimal;
8import java.math.BigInteger;
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +02009import java.util.Objects;
Matthias Andreas Benkardb8fbc372021-05-11 06:50:45 +020010import javax.json.Json;
11import javax.json.JsonObjectBuilder;
12import javax.json.JsonValue;
13
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020014/**
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020015 * A simple single key–value pair forming a {@link StructuredParameter}.
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020016 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020017 * <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 +020018 * {@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 Benkard42da9f12021-09-02 18:47:28 +020021 * <p><strong>Example:</strong>
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020022 *
23 * <pre>{@code
24 * logger.infof("Application starting.", StructuredParameter.of("version", "1.0"));
25 * }</pre>
26 *
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020027 * Result:
28 *
29 * <pre>{@code
30 * {
31 * "jsonPayload": {
32 * "message": "Application starting.",
33 * "version": "1.0"
34 * }
35 * }
36 * }</pre>
37 *
Matthias Andreas Benkard692f48d2021-08-31 21:06:50 +020038 * @see Label
39 * @see StructuredParameter
40 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +020041public 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 Benkardb8fbc372021-05-11 06:50:45 +020050
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020051 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020060 public static KeyValueParameter of(String key, String value) {
61 return new KeyValueParameter(key, Json.createValue(value));
62 }
63
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020064 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020073 public static KeyValueParameter of(String key, int value) {
74 return new KeyValueParameter(key, Json.createValue(value));
75 }
76
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020077 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020086 public static KeyValueParameter of(String key, long value) {
87 return new KeyValueParameter(key, Json.createValue(value));
88 }
89
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +020090 /**
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 Benkardb8fbc372021-05-11 06:50:45 +020099 public static KeyValueParameter of(String key, double value) {
100 return new KeyValueParameter(key, Json.createValue(value));
101 }
102
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200103 /**
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 Benkardb8fbc372021-05-11 06:50:45 +0200112 public static KeyValueParameter of(String key, BigDecimal value) {
113 return new KeyValueParameter(key, Json.createValue(value));
114 }
115
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200116 /**
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 Benkardb8fbc372021-05-11 06:50:45 +0200125 public static KeyValueParameter of(String key, BigInteger value) {
126 return new KeyValueParameter(key, Json.createValue(value));
127 }
128
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200129 /**
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 Benkardb8fbc372021-05-11 06:50:45 +0200138 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 Benkard121a6312021-05-12 05:41:25 +0200146
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200147 /**
148 * The key part of the key–value pair.
149 *
150 * @return the key part of the key–value pair.
151 */
Matthias Andreas Benkard121a6312021-05-12 05:41:25 +0200152 public String key() {
153 return key;
154 }
155
Matthias Andreas Benkard42da9f12021-09-02 18:47:28 +0200156 /**
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 Benkard121a6312021-05-12 05:41:25 +0200164 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 Benkardb8fbc372021-05-11 06:50:45 +0200185}