Skip to content

Commit 099fc28

Browse files
committed
Refinement and improvements.
1 parent 2195679 commit 099fc28

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

runtime/src/main/java/io/quarkiverse/loggingjson/LoggingJsonRecorder.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package io.quarkiverse.loggingjson;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.Optional;
3+
import java.util.*;
4+
import java.util.function.Supplier;
65
import java.util.logging.Formatter;
76
import java.util.stream.Collectors;
87

98
import org.slf4j.Logger;
109
import org.slf4j.LoggerFactory;
1110

12-
import com.fasterxml.jackson.databind.ObjectMapper;
13-
1411
import io.quarkiverse.loggingjson.config.Config;
1512
import io.quarkiverse.loggingjson.config.ConfigFormatter;
1613
import io.quarkiverse.loggingjson.jackson.JacksonJsonFactory;
@@ -24,7 +21,9 @@
2421
@Recorder
2522
public class LoggingJsonRecorder {
2623
private static final Logger log = LoggerFactory.getLogger(LoggingJsonRecorder.class);
27-
24+
private static final Map<Class<? extends JsonFactory>, Supplier<JsonFactory>> jsonFactories = Map.of(
25+
JacksonJsonFactory.class, LoggingJsonRecorder::initializeJacksonJsonFactory,
26+
JsonbJsonFactory.class, JsonbJsonFactory::new);
2827
private final RuntimeValue<Config> config;
2928

3029
public LoggingJsonRecorder(RuntimeValue<Config> config) {
@@ -48,7 +47,7 @@ public RuntimeValue<Optional<Formatter>> initializeJsonLogging(ConfigFormatter f
4847
if (formatter == null || !formatter.isEnabled()) {
4948
return new RuntimeValue<>(Optional.empty());
5049
}
51-
JsonFactory jsonFactory = initializeJsonFactory(jsonFactoryClass);
50+
JsonFactory jsonFactory = getJsonFactory(jsonFactoryClass);
5251
jsonFactory.setConfig(config);
5352

5453
List<JsonProvider> providers;
@@ -79,17 +78,21 @@ public RuntimeValue<Optional<Formatter>> initializeJsonLogging(ConfigFormatter f
7978
return new RuntimeValue<>(Optional.of(new JsonFormatter(providers, jsonFactory, config)));
8079
}
8180

82-
private JsonFactory initializeJsonFactory(Class<? extends JsonFactory> jsonFactoryType) {
83-
if (jsonFactoryType == JacksonJsonFactory.class) {
84-
ObjectMapperProvider objectMapperProvider = Arc.container().instance(ObjectMapperProvider.class).get();
85-
ObjectMapper objectMapper = objectMapperProvider.get();
86-
return new JacksonJsonFactory(objectMapper);
87-
} else if (jsonFactoryType == JsonbJsonFactory.class) {
88-
return new JsonbJsonFactory();
89-
} else {
90-
throw new RuntimeException(
91-
"Missing json implementation to use for logging-json. Supported: [quarkus-jackson, quarkus-jsonb]");
81+
private JsonFactory getJsonFactory(Class<? extends JsonFactory> jsonFactoryType) {
82+
Supplier<JsonFactory> jsonFactorySupplier = jsonFactories.get(jsonFactoryType);
83+
if (jsonFactorySupplier == null) {
84+
throw new IllegalArgumentException("Unsupported json factory type: " + jsonFactoryType);
85+
}
86+
return jsonFactorySupplier.get();
87+
}
88+
89+
private static JacksonJsonFactory initializeJacksonJsonFactory() {
90+
ObjectMapperProvider objectMapperProvider = Arc.container().instance(ObjectMapperProvider.class).orElse(null);
91+
if (objectMapperProvider != null) {
92+
return new JacksonJsonFactory(objectMapperProvider.get());
9293
}
94+
log.warn("ObjectMapperProvider not found, falling back to default ObjectMapper.");
95+
return new JacksonJsonFactory();
9396
}
9497

9598
private List<JsonProvider> defaultFormat(Config config) {

0 commit comments

Comments
 (0)